fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / tools / source / debug / debug.cxx
blob49da3089805f6ee5219a5d01c6cef2ac757acf67
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 #if defined (UNX) || defined (__GNUC__)
21 #include <unistd.h>
22 #else
23 #include <direct.h>
24 #endif
26 #include <errno.h>
27 #include <time.h>
28 #include <cstdarg> // combinations
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
33 #if defined ( WNT )
34 #include <windows.h>
35 #endif
37 #include <tools/debug.hxx>
38 #include <rtl/string.h>
39 #include <sal/log.hxx>
40 #include <sal/macros.h>
42 #include <vector>
44 #include <osl/diagnose.h>
45 #include <tools/diagnose_ex.h>
47 #ifdef DBG_UTIL
49 // PointerList
51 #define PBLOCKCOUNT 1024
53 struct PBlock
55 void* aData[PBLOCKCOUNT];
56 sal_uInt16 nCount;
57 PBlock* pPrev;
58 PBlock* pNext;
61 class PointerList
63 private:
64 PBlock* pFirst;
65 PBlock* pLast;
66 sal_uIntPtr nCount;
68 public:
69 PointerList() { pFirst = NULL; pLast = NULL; nCount = 0; }
70 ~PointerList();
72 void Add( const void* p );
73 sal_Bool Remove( const void* p );
75 const void* Get( sal_uIntPtr nPos ) const;
76 sal_Bool IsIn( const void* p ) const;
77 sal_uIntPtr Count() const { return nCount; }
80 // data types
82 #define DBG_MAXNAME 28
84 struct ProfType
86 sal_uIntPtr nCount;
87 sal_uIntPtr nTime;
88 sal_uIntPtr nMinTime;
89 sal_uIntPtr nMaxTime;
90 sal_uIntPtr nStart;
91 sal_uIntPtr nContinueTime;
92 sal_uIntPtr nContinueStart;
93 sal_Char aName[DBG_MAXNAME+1];
96 struct XtorType
98 sal_uIntPtr nCtorCalls;
99 sal_uIntPtr nDtorCalls;
100 sal_uIntPtr nMaxCount;
101 sal_uIntPtr nStatics;
102 sal_Char aName[DBG_MAXNAME+1];
103 sal_Bool bTest;
104 PointerList aThisList;
107 struct DebugData
109 DbgData aDbgData;
110 sal_uInt16 bInit;
111 DbgPrintLine pDbgPrintMsgBox;
112 DbgPrintLine pDbgPrintWindow;
113 DbgPrintLine pDbgPrintTestTool;
114 DbgPrintLine pDbgAbort;
115 ::std::vector< DbgPrintLine >
116 aDbgPrintUserChannels;
117 PointerList* pProfList;
118 PointerList* pXtorList;
119 DbgTestSolarMutexProc pDbgTestSolarMutex;
120 pfunc_osl_printDetailedDebugMessage
121 pOldDebugMessageFunc;
122 bool bOslIsHooked;
124 DebugData()
125 :bInit( sal_False )
126 ,pDbgPrintMsgBox( NULL )
127 ,pDbgPrintWindow( NULL )
128 ,pDbgPrintTestTool( NULL )
129 ,pDbgAbort( NULL )
130 ,pProfList( NULL )
131 ,pXtorList( NULL )
132 ,pDbgTestSolarMutex( NULL )
133 ,pOldDebugMessageFunc( NULL )
134 ,bOslIsHooked( false )
136 aDbgData.nTestFlags = DBG_TEST_RESOURCE;
137 aDbgData.bOverwrite = sal_True;
138 aDbgData.nTraceOut = DBG_OUT_NULL;
139 aDbgData.nWarningOut = DBG_OUT_NULL;
140 #ifdef UNX
141 aDbgData.nErrorOut = DBG_OUT_SHELL;
142 #else
143 aDbgData.nErrorOut = DBG_OUT_MSGBOX;
144 #endif
145 aDbgData.bHookOSLAssert = sal_True;
146 aDbgData.aDebugName[0] = 0;
147 aDbgData.aInclFilter[0] = 0;
148 aDbgData.aExclFilter[0] = 0;
149 aDbgData.aInclClassFilter[0] = 0;
150 aDbgData.aExclClassFilter[0] = 0;
151 aDbgData.aDbgWinState[0] = 0;
155 #define DBG_TEST_XTOR_EXTRA (DBG_TEST_XTOR_THIS | DBG_TEST_XTOR_FUNC | \
156 DBG_TEST_XTOR_EXIT | DBG_TEST_XTOR_REPORT )
158 // static maintenance variables
160 static DebugData aDebugData;
161 static sal_Char aCurPath[260];
162 static int bDbgImplInMain = sal_False;
164 #if defined( WNT )
165 static CRITICAL_SECTION aImplCritDbgSection;
166 #endif
168 static sal_Bool bImplCritDbgSectionInit = sal_False;
170 void ImplDbgInitLock()
172 #if defined( WNT )
173 InitializeCriticalSection( &aImplCritDbgSection );
174 #endif
175 bImplCritDbgSectionInit = sal_True;
178 void ImplDbgDeInitLock()
180 #if defined( WNT )
181 DeleteCriticalSection( &aImplCritDbgSection );
182 #endif
183 bImplCritDbgSectionInit = sal_False;
186 void ImplDbgLock()
188 if ( !bImplCritDbgSectionInit )
189 return;
191 #if defined( WNT )
192 EnterCriticalSection( &aImplCritDbgSection );
193 #endif
196 void ImplDbgUnlock()
198 if ( !bImplCritDbgSectionInit )
199 return;
201 #if defined( WNT )
202 LeaveCriticalSection( &aImplCritDbgSection );
203 #endif
206 #define FILE_LINEEND "\n"
208 static sal_Bool ImplActivateDebugger( const sal_Char* pMsg )
210 #if defined( WNT )
211 static sal_Char aImplDbgOutBuf[DBG_BUF_MAXLEN];
212 strcpy( aImplDbgOutBuf, pMsg );
213 strcat( aImplDbgOutBuf, "\r\n" );
214 OutputDebugString( aImplDbgOutBuf );
215 DebugBreak();
216 return sal_True;
217 #else
218 (void) pMsg; // avoid warning about unused parameter
219 return sal_False;
220 #endif
223 static sal_Bool ImplCoreDump()
225 #if defined( WNT )
226 DebugBreak();
227 #else
228 long* pTemp = 0;
229 *pTemp = 0xCCCC;
230 #endif
231 return sal_True;
234 static sal_uIntPtr ImplGetPerfTime()
236 #if defined( WNT )
237 return (sal_uIntPtr)GetTickCount();
238 #else
239 static sal_uIntPtr nImplTicksPerSecond = 0;
240 static double dImplTicksPerSecond;
241 sal_uIntPtr nTicks = (sal_uIntPtr)clock();
243 if ( !nImplTicksPerSecond )
245 nImplTicksPerSecond = CLOCKS_PER_SEC;
246 dImplTicksPerSecond = nImplTicksPerSecond;
249 double fTicks = nTicks;
250 fTicks *= 1000;
251 fTicks /= dImplTicksPerSecond;
252 return (sal_uIntPtr)fTicks;
253 #endif
256 typedef FILE* FILETYPE;
257 #define FileOpen fopen
258 #define FileRead fread
259 #define FilePrintF fprintf
260 #define FileClose fclose
262 namespace
264 enum ConfigSection
266 eOutput,
267 eMemory,
268 eGUI,
269 eObjects,
270 eTest,
272 eUnknown
275 void lcl_lineFeed( FILETYPE _pFile )
277 FilePrintF( _pFile, "%s", FILE_LINEEND );
280 const sal_Char* lcl_getSectionName( ConfigSection _eSection )
282 const sal_Char* pSectionName = NULL;
283 switch ( _eSection )
285 case eOutput : pSectionName = "output"; break;
286 case eMemory : pSectionName = "memory"; break;
287 case eGUI : pSectionName = "gui"; break;
288 case eObjects : pSectionName = "objects"; break;
289 case eTest : pSectionName = "test"; break;
290 case eUnknown:
291 OSL_ASSERT(false);
292 break;
294 return pSectionName;
297 ConfigSection lcl_getSectionFromName( const sal_Char* _pSectionName, size_t _nSectionNameLength )
299 if ( strncmp( _pSectionName, "output", _nSectionNameLength < 6 ? _nSectionNameLength : 6 ) == 0 )
300 return eOutput;
301 if ( strncmp( _pSectionName, "memory", _nSectionNameLength < 6 ? _nSectionNameLength : 6 ) == 0 )
302 return eMemory;
303 if ( strncmp( _pSectionName, "gui", _nSectionNameLength < 3 ? _nSectionNameLength : 3 ) == 0 )
304 return eGUI;
305 if ( strncmp( _pSectionName, "objects", _nSectionNameLength < 7 ? _nSectionNameLength : 7 ) == 0 )
306 return eObjects;
307 if ( strncmp( _pSectionName, "test", _nSectionNameLength < 4 ? _nSectionNameLength : 4 ) == 0 )
308 return eTest;
309 return eUnknown;
312 void lcl_startSection( FILETYPE _pFile, ConfigSection _eSection )
314 FilePrintF( _pFile, "[%s]%s", lcl_getSectionName( _eSection ), FILE_LINEEND );
317 void lcl_writeConfigString( FILETYPE _pFile, const sal_Char* _pKeyName, const sal_Char* _pValue )
319 FilePrintF( _pFile, "%s=%s%s", _pKeyName, _pValue, FILE_LINEEND );
322 void lcl_writeConfigBoolean( FILETYPE _pFile, const sal_Char* _pKeyName, bool _bValue )
324 lcl_writeConfigString( _pFile, _pKeyName, _bValue ? "1" : "0" );
327 void lcl_writeConfigFlag( FILETYPE _pFile, const sal_Char* _pKeyName, sal_uIntPtr _nAllFlags, sal_uIntPtr _nCheckFlag )
329 lcl_writeConfigBoolean( _pFile, _pKeyName, ( _nAllFlags & _nCheckFlag ) != 0 );
332 void lcl_writeConfigOutChannel( FILETYPE _pFile, const sal_Char* _pKeyName, sal_uIntPtr _nValue )
334 const sal_Char* names[ DBG_OUT_COUNT ] =
336 "dev/null", "file", "window", "shell", "messagebox", "testtool", "debugger", "abort"
338 lcl_writeConfigString( _pFile, _pKeyName, names[ _nValue ] );
341 bool lcl_isConfigSection( const sal_Char* _pLine, size_t _nLineLen )
343 if ( _nLineLen < 2 )
344 // not even enough space for '[' and ']'
345 return false;
346 if ( ( _pLine[0] == '[' ) && ( _pLine[ _nLineLen - 1 ] == ']' ) )
347 return true;
348 return false;
351 bool lcl_isConfigKey( const sal_Char* _pLine, size_t _nLineLen, const sal_Char* _pKeyName )
353 size_t nKeyLength = strlen( _pKeyName );
354 if ( nKeyLength + 1 >= _nLineLen )
355 // not even long enough for the key name plus "=" plus a one-character value
356 return false;
357 if ( ( strncmp( _pLine, _pKeyName, nKeyLength ) == 0 ) && ( _pLine[ nKeyLength ] == '=' ) )
358 return true;
359 return false;
362 sal_Int32 lcl_tryReadConfigString( const sal_Char* _pLine, size_t _nLineLen, const sal_Char* _pKeyName, sal_Char* _pValue, size_t _nValueLen )
364 if ( !lcl_isConfigKey( _pLine, _nLineLen, _pKeyName ) )
365 return 0;
366 size_t nValuePos = strlen( _pKeyName ) + 1;
367 size_t nValueLen = _nLineLen - nValuePos;
368 const sal_Char* pValue = _pLine + nValuePos;
369 strncpy( _pValue, pValue, ( _nValueLen > nValueLen ) ? nValueLen : _nValueLen );
370 _pValue[ ( _nValueLen > nValueLen ) ? nValueLen : _nValueLen - 1 ] = 0;
371 return strlen( _pValue );
374 void lcl_tryReadConfigBoolean( const sal_Char* _pLine, size_t _nLineLen, const sal_Char* _pKeyName, sal_uIntPtr* _out_pnValue )
376 sal_Char aBuf[2];
377 size_t nValueLen = lcl_tryReadConfigString( _pLine, _nLineLen, _pKeyName, aBuf, sizeof( aBuf ) );
378 if ( nValueLen )
379 *_out_pnValue = strcmp( aBuf, "1" ) == 0 ? sal_True : sal_False;
382 void lcl_matchOutputChannel( sal_Char const * i_buffer, sal_uIntPtr* o_value )
384 if ( i_buffer == NULL )
385 return;
386 const sal_Char* names[ DBG_OUT_COUNT ] =
388 "dev/null", "file", "window", "shell", "messagebox", "testtool", "debugger", "abort"
390 for ( size_t name = 0; name < SAL_N_ELEMENTS( names ); ++name )
392 if ( strcmp( i_buffer, names[ name ] ) == 0 )
394 *o_value = name;
395 return;
400 void lcl_tryReadOutputChannel( const sal_Char* _pLine, size_t _nLineLen, const sal_Char* _pKeyName, sal_uIntPtr* _out_pnValue )
402 sal_Char aBuf[20];
403 size_t nValueLen = lcl_tryReadConfigString( _pLine, _nLineLen, _pKeyName, aBuf, sizeof( aBuf ) );
404 if ( nValueLen )
405 lcl_matchOutputChannel( aBuf, _out_pnValue );
408 void lcl_tryReadConfigFlag( const sal_Char* _pLine, size_t _nLineLen, const sal_Char* _pKeyName, sal_uIntPtr* _out_pnAllFlags, sal_uIntPtr _nCheckFlag )
410 sal_Char aBuf[2];
411 size_t nValueLen = lcl_tryReadConfigString( _pLine, _nLineLen, _pKeyName, aBuf, sizeof( aBuf ) );
412 if ( nValueLen )
414 if ( strcmp( aBuf, "1" ) == 0 )
415 *_out_pnAllFlags |= _nCheckFlag;
416 else
417 *_out_pnAllFlags &= ~_nCheckFlag;
422 PointerList::~PointerList()
424 PBlock* pBlock = pFirst;
425 while ( pBlock )
427 PBlock* pNextBlock = pBlock->pNext;
428 delete pBlock;
429 pBlock = pNextBlock;
433 void PointerList::Add( const void* p )
435 if ( !pFirst )
437 pFirst = new PBlock;
438 memset( pFirst->aData, 0, PBLOCKCOUNT * sizeof( void* ) );
439 pFirst->nCount = 0;
440 pFirst->pPrev = NULL;
441 pFirst->pNext = NULL;
442 pLast = pFirst;
445 PBlock* pBlock = pFirst;
446 while ( pBlock && (pBlock->nCount == PBLOCKCOUNT) )
447 pBlock = pBlock->pNext;
449 if ( !pBlock )
451 pBlock = new PBlock;
452 memset( pBlock->aData, 0, PBLOCKCOUNT * sizeof( void* ) );
453 pBlock->nCount = 0;
454 pBlock->pPrev = pLast;
455 pBlock->pNext = NULL;
456 pLast->pNext = pBlock;
457 pLast = pBlock;
460 sal_uInt16 i = 0;
461 while ( pBlock->aData[i] )
462 i++;
464 pBlock->aData[i] = (void*)p;
465 pBlock->nCount++;
466 nCount++;
469 sal_Bool PointerList::Remove( const void* p )
471 if ( !p )
472 return sal_False;
474 PBlock* pBlock = pFirst;
475 while ( pBlock )
477 sal_uInt16 i = 0;
478 while ( i < PBLOCKCOUNT )
480 if ( ((sal_uIntPtr)p) == ((sal_uIntPtr)pBlock->aData[i]) )
482 pBlock->aData[i] = NULL;
483 pBlock->nCount--;
484 nCount--;
486 if ( !pBlock->nCount )
488 if ( pBlock->pPrev )
489 pBlock->pPrev->pNext = pBlock->pNext;
490 if ( pBlock->pNext )
491 pBlock->pNext->pPrev = pBlock->pPrev;
492 if ( pBlock == pFirst )
493 pFirst = pBlock->pNext;
494 if ( pBlock == pLast )
495 pLast = pBlock->pPrev;
496 delete pBlock;
499 return sal_True;
501 i++;
504 pBlock = pBlock->pNext;
507 return sal_False;
510 const void* PointerList::Get( sal_uIntPtr nPos ) const
512 if ( nCount <= nPos )
513 return NULL;
515 PBlock* pBlock = pFirst;
516 sal_uIntPtr nStart = 0;
517 while ( pBlock )
519 sal_uInt16 i = 0;
520 while ( i < PBLOCKCOUNT )
522 if ( pBlock->aData[i] )
524 nStart++;
525 if ( (nStart-1) == nPos )
526 return pBlock->aData[i];
529 i++;
532 pBlock = pBlock->pNext;
535 return NULL;
538 sal_Bool PointerList::IsIn( const void* p ) const
540 if ( !p )
541 return sal_False;
543 PBlock* pBlock = pFirst;
544 while ( pBlock )
546 sal_uInt16 i = 0;
547 while ( i < PBLOCKCOUNT )
549 if ( ((sal_uIntPtr)p) == ((sal_uIntPtr)pBlock->aData[i]) )
550 return sal_True;
551 i++;
554 pBlock = pBlock->pNext;
557 return sal_False;
560 static void DbgGetDbgFileName( sal_Char* pStr, sal_Int32 nMaxLen )
562 #if defined( UNX )
563 const sal_Char* pName = getenv("DBGSV_INIT");
564 if ( !pName )
565 pName = ".dbgsv.init";
566 strncpy( pStr, pName, nMaxLen );
567 #elif defined( WNT )
568 const sal_Char* pName = getenv("DBGSV_INIT");
569 if ( pName )
570 strncpy( pStr, pName, nMaxLen );
571 else
572 GetProfileStringA( "sv", "dbgsv", "dbgsv.ini", pStr, nMaxLen );
573 #else
574 strncpy( pStr, "dbgsv.ini", nMaxLen );
575 #endif
576 pStr[ nMaxLen - 1 ] = 0;
579 static void DbgGetLogFileName( sal_Char* pStr )
581 #if defined( UNX )
582 const sal_Char* pName = getenv("DBGSV_LOG");
583 if ( !pName )
584 pName = "dbgsv.log";
585 strcpy( pStr, pName );
586 #elif defined( WNT )
587 const sal_Char* pName = getenv("DBGSV_LOG");
588 if ( pName )
589 strcpy( pStr, pName );
590 else
591 GetProfileStringA( "sv", "dbgsvlog", "dbgsv.log", pStr, 200 );
592 #else
593 strcpy( pStr, "dbgsv.log" );
594 #endif
597 static DebugData* GetDebugData()
599 if ( !aDebugData.bInit )
601 aDebugData.bInit = sal_True;
603 // set default debug names
604 DbgGetLogFileName( aDebugData.aDbgData.aDebugName );
606 // DEBUG.INI-File
607 sal_Char aBuf[ 4096 ];
608 DbgGetDbgFileName( aBuf, sizeof( aBuf ) );
609 FILETYPE pIniFile = FileOpen( aBuf, "r" );
610 if ( pIniFile != NULL )
612 ConfigSection eCurrentSection = eUnknown;
614 // no sophisticated algorithm here, assume that the whole file fits into aBuf ...
615 sal_uIntPtr nReallyRead = FileRead( aBuf, 1, sizeof( aBuf ) / sizeof( sal_Char ) - 1, pIniFile );
616 aBuf[ nReallyRead ] = 0;
617 const sal_Char* pLine = aBuf;
618 while ( const sal_Char* pNextLine = strstr( pLine, FILE_LINEEND ) )
620 size_t nLineLength = pNextLine - pLine;
622 if ( lcl_isConfigSection( pLine, nLineLength ) )
623 eCurrentSection = lcl_getSectionFromName( pLine + 1, nLineLength - 2 );
625 // elements of the [output] section
626 if ( eCurrentSection == eOutput )
628 lcl_tryReadConfigString( pLine, nLineLength, "log_file", aDebugData.aDbgData.aDebugName, sizeof( aDebugData.aDbgData.aDebugName ) );
629 lcl_tryReadConfigBoolean( pLine, nLineLength, "overwrite", &aDebugData.aDbgData.bOverwrite );
630 lcl_tryReadConfigString( pLine, nLineLength, "include", aDebugData.aDbgData.aInclFilter, sizeof( aDebugData.aDbgData.aInclFilter ) );
631 lcl_tryReadConfigString( pLine, nLineLength, "exclude", aDebugData.aDbgData.aExclFilter, sizeof( aDebugData.aDbgData.aExclFilter ) );
632 lcl_tryReadConfigString( pLine, nLineLength, "include_class", aDebugData.aDbgData.aInclClassFilter, sizeof( aDebugData.aDbgData.aInclClassFilter ) );
633 lcl_tryReadConfigString( pLine, nLineLength, "exclude_class", aDebugData.aDbgData.aExclClassFilter, sizeof( aDebugData.aDbgData.aExclClassFilter ) );
634 lcl_tryReadOutputChannel( pLine, nLineLength, "trace", &aDebugData.aDbgData.nTraceOut );
635 lcl_tryReadOutputChannel( pLine, nLineLength, "warning", &aDebugData.aDbgData.nWarningOut );
636 lcl_tryReadOutputChannel( pLine, nLineLength, "error", &aDebugData.aDbgData.nErrorOut );
637 lcl_tryReadConfigBoolean( pLine, nLineLength, "oslhook", &aDebugData.aDbgData.bHookOSLAssert );
640 // elements of the [gui] section
641 if ( eCurrentSection == eGUI )
643 lcl_tryReadConfigString( pLine, nLineLength, "debug_window_state", aDebugData.aDbgData.aDbgWinState, sizeof( aDebugData.aDbgData.aDbgWinState ) );
646 // elements of the [objects] section
647 if ( eCurrentSection == eObjects )
649 lcl_tryReadConfigFlag( pLine, nLineLength, "check_this", &aDebugData.aDbgData.nTestFlags, DBG_TEST_XTOR_THIS );
650 lcl_tryReadConfigFlag( pLine, nLineLength, "check_function", &aDebugData.aDbgData.nTestFlags, DBG_TEST_XTOR_FUNC );
651 lcl_tryReadConfigFlag( pLine, nLineLength, "check_exit", &aDebugData.aDbgData.nTestFlags, DBG_TEST_XTOR_EXIT );
652 lcl_tryReadConfigFlag( pLine, nLineLength, "generate_report", &aDebugData.aDbgData.nTestFlags, DBG_TEST_XTOR_REPORT );
653 lcl_tryReadConfigFlag( pLine, nLineLength, "trace", &aDebugData.aDbgData.nTestFlags, DBG_TEST_XTOR_TRACE );
656 // elements of the [test] section
657 if ( eCurrentSection == eTest )
659 lcl_tryReadConfigFlag( pLine, nLineLength, "profiling", &aDebugData.aDbgData.nTestFlags, DBG_TEST_PROFILING );
660 lcl_tryReadConfigFlag( pLine, nLineLength, "resources", &aDebugData.aDbgData.nTestFlags, DBG_TEST_RESOURCE );
661 lcl_tryReadConfigFlag( pLine, nLineLength, "dialog", &aDebugData.aDbgData.nTestFlags, DBG_TEST_DIALOG );
662 lcl_tryReadConfigFlag( pLine, nLineLength, "bold_app_font", &aDebugData.aDbgData.nTestFlags, DBG_TEST_BOLDAPPFONT );
665 pLine = pNextLine + strlen( FILE_LINEEND );
668 FileClose( pIniFile );
670 else
672 lcl_matchOutputChannel( getenv( "DBGSV_TRACE_OUT" ), &aDebugData.aDbgData.nTraceOut );
673 lcl_matchOutputChannel( getenv( "DBGSV_WARNING_OUT" ), &aDebugData.aDbgData.nWarningOut );
674 lcl_matchOutputChannel( getenv( "DBGSV_ERROR_OUT" ), &aDebugData.aDbgData.nErrorOut );
678 sal_Char* getcwdResult = getcwd( aCurPath, sizeof( aCurPath ) );
679 if ( !getcwdResult )
681 OSL_TRACE( "getcwd failed with error %s", strerror(errno) );
684 // initialize debug data
685 if ( aDebugData.aDbgData.nTestFlags & DBG_TEST_XTOR )
686 aDebugData.pXtorList = new PointerList;
687 if ( aDebugData.aDbgData.nTestFlags & DBG_TEST_PROFILING )
688 aDebugData.pProfList = new PointerList;
691 return &aDebugData;
694 inline DebugData* ImplGetDebugData()
696 if ( !aDebugData.bInit )
697 return GetDebugData();
698 else
699 return &aDebugData;
702 static FILETYPE ImplDbgInitFile()
704 static sal_Bool bFileInit = sal_False;
706 sal_Char aBuf[4096];
707 sal_Char* getcwdResult = getcwd( aBuf, sizeof( aBuf ) );
708 if ( !getcwdResult ) {
709 OSL_TRACE( "getcwd failed with error = %s", strerror(errno) );
710 return NULL;
713 int chdirResult = chdir( aCurPath );
714 if ( !chdirResult ) {
715 OSL_TRACE ( "chdir failed with error = %s", strerror(errno) );
716 return NULL;
719 DebugData* pData = GetDebugData();
720 FILETYPE pDebugFile;
722 if ( !bFileInit )
724 bFileInit = sal_True;
726 if ( pData->aDbgData.bOverwrite )
727 pDebugFile = FileOpen( pData->aDbgData.aDebugName, "w" );
728 else
729 pDebugFile = FileOpen( pData->aDbgData.aDebugName, "a" );
731 if ( pDebugFile )
733 time_t nTime = time( 0 );
734 tm* pTime;
735 #ifdef UNX
736 tm aTime;
737 pTime = localtime_r( &nTime, &aTime );
738 #else
739 pTime = localtime( &nTime );
740 #endif
742 // print header
743 FilePrintF( pDebugFile, "******************************************************************************%s", FILE_LINEEND );
744 FilePrintF( pDebugFile, "%s%s", pData->aDbgData.aDebugName, FILE_LINEEND );
745 if ( pTime )
746 FilePrintF( pDebugFile, "%s%s", asctime( pTime ), FILE_LINEEND );
749 else
750 pDebugFile = FileOpen( pData->aDbgData.aDebugName, "a" );
752 chdirResult = chdir( aBuf );
753 if ( !chdirResult )
755 OSL_TRACE( "chdir failed with error = %s", strerror(errno) );
758 return pDebugFile;
761 static void ImplDbgPrintFile( const sal_Char* pLine )
763 FILETYPE pDebugFile = ImplDbgInitFile();
765 if ( pDebugFile )
767 FilePrintF( pDebugFile, "%s%s", pLine, FILE_LINEEND );
768 FileClose( pDebugFile );
772 static int ImplStrSearch( const sal_Char* pSearchStr, int nSearchLen,
773 const sal_Char* pStr, int nLen )
775 int nPos = 0;
776 while ( nPos+nSearchLen <= nLen )
778 if ( strncmp( pStr+nPos, pSearchStr, nSearchLen ) == 0 )
779 return 1;
780 nPos++;
783 return 0;
786 static int ImplDbgFilter( const sal_Char* pFilter, const sal_Char* pMsg,
787 int bEmpty )
789 int nStrLen = strlen( pFilter );
790 if ( !nStrLen )
791 return bEmpty;
793 int nMsgLen = strlen( pMsg );
794 const sal_Char* pTok = pFilter;
795 int nTok = 0;
796 while ( pTok[nTok] )
798 if ( pTok[nTok] == ';' )
800 if ( nTok && ImplStrSearch( pTok, nTok, pMsg, nMsgLen ) )
801 return sal_True;
803 pTok += nTok+1;
804 nTok = 0;
807 nTok++;
810 if ( nTok && ImplStrSearch( pTok, nTok, pMsg, nMsgLen ) )
811 return sal_True;
812 else
813 return sal_False;
816 extern "C"
817 void SAL_CALL dbg_printOslDebugMessage( const sal_Char * pszFileName, sal_Int32 nLine, const sal_Char * pszMessage )
819 DbgOut( pszMessage ? pszMessage : "assertion failed!", DBG_OUT_ERROR, pszFileName, (sal_uInt16)nLine );
822 static void DebugInit()
824 bDbgImplInMain = sal_True;
825 ImplDbgInitLock();
827 DebugData* pData = GetDebugData();
828 if( pData->aDbgData.bHookOSLAssert && ! pData->bOslIsHooked )
830 pData->pOldDebugMessageFunc = osl_setDetailedDebugMessageFunc( &dbg_printOslDebugMessage );
831 pData->bOslIsHooked = true;
835 static void DebugDeInit()
837 DebugData* pData = GetDebugData();
838 sal_uIntPtr i;
839 sal_uIntPtr nCount;
840 sal_uIntPtr nOldOut;
842 if( pData->bOslIsHooked )
844 osl_setDetailedDebugMessageFunc( pData->pOldDebugMessageFunc );
845 pData->bOslIsHooked = sal_False;
848 // Output statistics trace data to file
849 nOldOut = pData->aDbgData.nTraceOut;
850 pData->aDbgData.nTraceOut = DBG_OUT_FILE;
852 // output Xtor list
853 if ( pData->pXtorList && pData->pXtorList->Count() &&
854 (pData->aDbgData.nTestFlags & DBG_TEST_XTOR_REPORT) )
856 DbgOutf( "------------------------------------------------------------------------------" );
857 DbgOutf( "Object Report" );
858 DbgOutf( "------------------------------------------------------------------------------" );
859 DbgOutf( "%-27s : %-9s : %-9s : %-7s : %-3s : %-6s :",
860 "XTor-List", "Ctor", "Dtor", "MaxInst", "St.", "Diff." );
861 DbgOutf( "----------------------------:-----------:-----------:---------:----:---------:" );
862 for( i = 0, nCount = pData->pXtorList->Count(); i < nCount; i++ )
864 XtorType* pXtorData = (XtorType*)pData->pXtorList->Get( i );
865 if ( pXtorData->bTest )
867 // Add static objects
868 pXtorData->nDtorCalls += pXtorData->nStatics;
869 if ( pXtorData->nStatics && (pXtorData->nDtorCalls > pXtorData->nCtorCalls) )
870 pXtorData->nDtorCalls = pXtorData->nCtorCalls;
871 DbgOutf( "%-27s : %9lu : %9lu : %7lu : %3lu : %4lu %-1s :",
872 pXtorData->aName, pXtorData->nCtorCalls, pXtorData->nDtorCalls,
873 pXtorData->nMaxCount, pXtorData->nStatics,
874 pXtorData->nCtorCalls - pXtorData->nDtorCalls,
875 (pXtorData->nCtorCalls - pXtorData->nDtorCalls) ? "!" : " " );
878 DbgOutf( "==============================================================================" );
881 // free XtorList
882 if ( pData->pXtorList )
884 for( i = 0, nCount = pData->pXtorList->Count(); i < nCount; i++ )
886 XtorType* pXtorData = (XtorType*)pData->pXtorList->Get( i );
887 delete pXtorData;
889 delete pData->pXtorList;
890 pData->pXtorList = NULL;
893 // Set everything to sal_False, as global variables
894 // may cause a system crash otherwise.
895 // Maintain memory flags, as otherwise new/delete calls
896 // for global variables will crash,
897 // as pointer alignment won't work then.
898 pData->aDbgData.nTraceOut = nOldOut;
899 pData->aDbgData.nTestFlags &= DBG_TEST_PROFILING;
900 pData->aDbgPrintUserChannels.clear();
901 pData->pDbgPrintTestTool = NULL;
902 pData->pDbgPrintWindow = NULL;
903 pData->pOldDebugMessageFunc = NULL;
904 ImplDbgDeInitLock();
907 static void DebugGlobalDeInit()
909 DebugData* pData = GetDebugData();
910 sal_uIntPtr i;
911 sal_uIntPtr nCount;
912 sal_uIntPtr nOldOut;
914 // Output statistics trace data to file
915 nOldOut = pData->aDbgData.nTraceOut;
916 pData->aDbgData.nTraceOut = DBG_OUT_FILE;
918 // output profile liste
919 if ( pData->pProfList && pData->pProfList->Count() )
921 DbgOutf( "------------------------------------------------------------------------------" );
922 DbgOutf( "Profiling Report" );
923 DbgOutf( "------------------------------------------------------------------------------" );
924 DbgOutf( "%-25s : %-9s : %-6s : %-6s : %-6s : %-9s :",
925 "Prof-List (ms)", "Time", "Min", "Max", "Ave", "Count" );
926 DbgOutf( "--------------------------:-----------:--------:--------:--------:-----------:" );
927 for( i = 0, nCount = pData->pProfList->Count(); i < nCount; i++ )
929 ProfType* pProfData = (ProfType*)pData->pProfList->Get( i );
930 sal_uIntPtr nAve = pProfData->nTime / pProfData->nCount;
931 DbgOutf( "%-25s : %9lu : %6lu : %6lu : %6lu : %9lu :",
932 pProfData->aName, pProfData->nTime,
933 pProfData->nMinTime, pProfData->nMaxTime, nAve,
934 pProfData->nCount );
936 DbgOutf( "==============================================================================" );
939 // free profile list
940 if ( pData->pProfList )
942 for( i = 0, nCount = pData->pProfList->Count(); i < nCount; i++ )
944 ProfType* pProfData = (ProfType*)pData->pProfList->Get( i );
945 delete pProfData;
947 delete pData->pProfList;
948 pData->pProfList = NULL;
951 // disable profiling flags
952 pData->aDbgData.nTraceOut = nOldOut;
953 pData->aDbgData.nTestFlags &= ~DBG_TEST_PROFILING;
956 void ImpDbgOutfBuf( sal_Char* pBuf, const sal_Char* pFStr, ... )
958 va_list pList;
960 va_start( pList, pFStr );
961 sal_Char aBuf[DBG_BUF_MAXLEN];
962 vsprintf( aBuf, pFStr, pList );
963 va_end( pList );
965 strcat( pBuf, aBuf );
966 strcat( pBuf, "\n" );
969 static void DebugXTorInfo( sal_Char* pBuf )
971 DebugData* pData = GetDebugData();
972 sal_uIntPtr i;
973 sal_uIntPtr nCount;
975 // output Xtor list
976 if ( pData->pXtorList && pData->pXtorList->Count() &&
977 (pData->aDbgData.nTestFlags & DBG_TEST_XTOR_REPORT) )
979 ImpDbgOutfBuf( pBuf, "------------------------------------------------------------------------------" );
980 ImpDbgOutfBuf( pBuf, "Object Report" );
981 ImpDbgOutfBuf( pBuf, "------------------------------------------------------------------------------" );
982 ImpDbgOutfBuf( pBuf, "%-27s : %-9s : %-9s : %-7s : %-3s : %-6s :",
983 "XTor-List", "Ctor", "Dtor", "MaxInst", "St.", "Diff." );
984 ImpDbgOutfBuf( pBuf, "----------------------------:-----------:-----------:---------:----:---------:" );
985 for( i = 0, nCount = pData->pXtorList->Count(); i < nCount; i++ )
987 XtorType* pXtorData = (XtorType*)pData->pXtorList->Get( i );
988 if ( pXtorData->bTest )
990 ImpDbgOutfBuf( pBuf, "%-27s : %9lu : %9lu : %7lu : %3lu : %6lu :",
991 pXtorData->aName, pXtorData->nCtorCalls, pXtorData->nDtorCalls,
992 pXtorData->nMaxCount, pXtorData->nStatics,
993 pXtorData->nCtorCalls - pXtorData->nDtorCalls );
996 ImpDbgOutfBuf( pBuf, "==============================================================================" );
997 ImpDbgOutfBuf( pBuf, "" );
1001 sal_Bool ImplDbgFilterMessage( const sal_Char* pMsg )
1003 DebugData* pData = GetDebugData();
1004 if ( !ImplDbgFilter( pData->aDbgData.aInclFilter, pMsg, sal_True ) )
1005 return sal_True;
1006 if ( ImplDbgFilter( pData->aDbgData.aExclFilter, pMsg, sal_False ) )
1007 return sal_True;
1008 return sal_False;
1011 void* DbgFunc( sal_uInt16 nAction, void* pParam )
1013 DebugData* pDebugData = ImplGetDebugData();
1015 if ( nAction == DBG_FUNC_GETDATA )
1016 return (void*)&(pDebugData->aDbgData);
1017 else if ( nAction == DBG_FUNC_GETPRINTMSGBOX )
1018 return (void*)(long)(pDebugData->pDbgPrintMsgBox);
1019 else if ( nAction == DBG_FUNC_FILTERMESSAGE )
1020 if ( ImplDbgFilterMessage( (const sal_Char*) pParam ) )
1021 return (void*) -1;
1022 else
1023 return (void*) 0; // aka NULL
1024 else
1027 switch ( nAction )
1029 case DBG_FUNC_DEBUGSTART:
1030 DebugInit();
1031 break;
1033 case DBG_FUNC_DEBUGEND:
1034 DebugDeInit();
1035 break;
1037 case DBG_FUNC_GLOBALDEBUGEND:
1038 DebugGlobalDeInit();
1039 break;
1041 case DBG_FUNC_SETPRINTMSGBOX:
1042 pDebugData->pDbgPrintMsgBox = (DbgPrintLine)(long)pParam;
1043 break;
1045 case DBG_FUNC_SETPRINTWINDOW:
1046 pDebugData->pDbgPrintWindow = (DbgPrintLine)(long)pParam;
1047 break;
1049 case DBG_FUNC_SETPRINTTESTTOOL:
1050 pDebugData->pDbgPrintTestTool = (DbgPrintLine)(long)pParam;
1051 break;
1053 case DBG_FUNC_SET_ABORT:
1054 pDebugData->pDbgAbort = (DbgPrintLine)(long)pParam;
1055 break;
1057 case DBG_FUNC_SAVEDATA:
1059 const DbgData* pData = static_cast< const DbgData* >( pParam );
1061 sal_Char aBuf[ 4096 ];
1062 DbgGetDbgFileName( aBuf, sizeof( aBuf ) );
1063 FILETYPE pIniFile = FileOpen( aBuf, "w" );
1064 if ( pIniFile == NULL )
1065 break;
1067 lcl_startSection( pIniFile, eOutput );
1068 lcl_writeConfigString( pIniFile, "log_file", pData->aDebugName );
1069 lcl_writeConfigBoolean( pIniFile, "overwrite", pData->bOverwrite );
1070 lcl_writeConfigString( pIniFile, "include", pData->aInclFilter );
1071 lcl_writeConfigString( pIniFile, "exclude", pData->aExclFilter );
1072 lcl_writeConfigString( pIniFile, "include_class", pData->aInclClassFilter );
1073 lcl_writeConfigString( pIniFile, "exclude_class", pData->aExclClassFilter );
1074 lcl_writeConfigOutChannel( pIniFile, "trace", pData->nTraceOut );
1075 lcl_writeConfigOutChannel( pIniFile, "warning", pData->nWarningOut );
1076 lcl_writeConfigOutChannel( pIniFile, "error", pData->nErrorOut );
1077 lcl_writeConfigBoolean( pIniFile, "oslhook", pData->bHookOSLAssert );
1079 lcl_lineFeed( pIniFile );
1080 lcl_startSection( pIniFile, eGUI );
1081 lcl_writeConfigString( pIniFile, "debug_window_state", pData->aDbgWinState );
1083 lcl_lineFeed( pIniFile );
1084 lcl_startSection( pIniFile, eObjects );
1085 lcl_writeConfigFlag( pIniFile, "check_this", pData->nTestFlags, DBG_TEST_XTOR_THIS );
1086 lcl_writeConfigFlag( pIniFile, "check_function", pData->nTestFlags, DBG_TEST_XTOR_FUNC );
1087 lcl_writeConfigFlag( pIniFile, "check_exit", pData->nTestFlags, DBG_TEST_XTOR_EXIT );
1088 lcl_writeConfigFlag( pIniFile, "generate_report", pData->nTestFlags, DBG_TEST_XTOR_REPORT );
1089 lcl_writeConfigFlag( pIniFile, "trace", pData->nTestFlags, DBG_TEST_XTOR_TRACE );
1091 lcl_lineFeed( pIniFile );
1092 lcl_startSection( pIniFile, eTest );
1093 lcl_writeConfigFlag( pIniFile, "profiling", pData->nTestFlags, DBG_TEST_PROFILING );
1094 lcl_writeConfigFlag( pIniFile, "resources", pData->nTestFlags, DBG_TEST_RESOURCE );
1095 lcl_writeConfigFlag( pIniFile, "dialog", pData->nTestFlags, DBG_TEST_DIALOG );
1096 lcl_writeConfigFlag( pIniFile, "bold_app_font", pData->nTestFlags, DBG_TEST_BOLDAPPFONT );
1098 FileClose( pIniFile );
1100 break;
1102 case DBG_FUNC_XTORINFO:
1103 DebugXTorInfo( (sal_Char*)pParam );
1104 break;
1106 case DBG_FUNC_COREDUMP:
1107 ImplCoreDump();
1108 break;
1110 case DBG_FUNC_ALLERROROUT:
1111 return (void*)(sal_uIntPtr)sal_True;
1113 case DBG_FUNC_SETTESTSOLARMUTEX:
1114 pDebugData->pDbgTestSolarMutex = (DbgTestSolarMutexProc)(long)pParam;
1115 break;
1117 case DBG_FUNC_TESTSOLARMUTEX:
1118 if ( pDebugData->pDbgTestSolarMutex )
1119 pDebugData->pDbgTestSolarMutex();
1120 break;
1122 case DBG_FUNC_PRINTFILE:
1123 ImplDbgPrintFile( (const sal_Char*)pParam );
1124 break;
1125 case DBG_FUNC_UPDATEOSLHOOK:
1127 const DbgData* pData = static_cast< const DbgData* >( pParam );
1128 pDebugData->aDbgData.bHookOSLAssert = pData->bHookOSLAssert;
1129 if( pDebugData->bOslIsHooked && ! pData->bHookOSLAssert )
1131 osl_setDetailedDebugMessageFunc( pDebugData->pOldDebugMessageFunc );
1132 pDebugData->bOslIsHooked = sal_False;
1134 else if( ! pDebugData->bOslIsHooked && pData->bHookOSLAssert )
1136 pDebugData->pOldDebugMessageFunc = osl_setDetailedDebugMessageFunc( &dbg_printOslDebugMessage );
1137 pDebugData->bOslIsHooked = sal_True;
1140 break;
1143 return NULL;
1147 DbgChannelId DbgRegisterUserChannel( DbgPrintLine pProc )
1149 DebugData* pData = ImplGetDebugData();
1150 pData->aDbgPrintUserChannels.push_back( pProc );
1151 return (DbgChannelId)( pData->aDbgPrintUserChannels.size() - 1 + DBG_OUT_USER_CHANNEL_0 );
1154 void DbgProf( sal_uInt16 nAction, DbgDataType* pDbgData )
1156 DebugData* pData = ImplGetDebugData();
1158 if ( !(pData->aDbgData.nTestFlags & DBG_TEST_PROFILING) )
1159 return;
1161 ProfType* pProfData = (ProfType*)pDbgData->pData;
1162 sal_uIntPtr nTime;
1163 if ( (nAction != DBG_PROF_START) && !pProfData )
1165 SAL_WARN(
1166 "tools.debug",
1167 "DBG_PROF...() without DBG_PROFSTART(): " << pDbgData->pName);
1168 return;
1171 switch ( nAction )
1173 case DBG_PROF_START:
1174 if ( !pDbgData->pData )
1176 pDbgData->pData = (void*)new ProfType;
1177 pProfData = (ProfType*)pDbgData->pData;
1178 strncpy( pProfData->aName, pDbgData->pName, DBG_MAXNAME );
1179 pProfData->aName[DBG_MAXNAME] = '\0';
1180 pProfData->nCount = 0;
1181 pProfData->nTime = 0;
1182 pProfData->nMinTime = 0xFFFFFFFF;
1183 pProfData->nMaxTime = 0;
1184 pProfData->nStart = 0xFFFFFFFF;
1185 pProfData->nContinueTime = 0;
1186 pProfData->nContinueStart = 0xFFFFFFFF;
1187 pData->pProfList->Add( (void*)pProfData );
1190 if ( pProfData->nStart == 0xFFFFFFFF )
1192 pProfData->nStart = ImplGetPerfTime();
1193 pProfData->nCount++;
1195 break;
1197 case DBG_PROF_STOP:
1198 nTime = ImplGetPerfTime();
1200 if ( pProfData->nStart == 0xFFFFFFFF )
1202 SAL_WARN(
1203 "tools.debug", "DBG_PROF...() without DBG_PROFSTART()");
1204 return;
1207 if ( pProfData->nContinueStart != 0xFFFFFFFF )
1209 pProfData->nContinueTime += ImplGetPerfTime() - pProfData->nContinueStart;
1210 pProfData->nContinueStart = 0xFFFFFFFF;
1213 nTime -= pProfData->nStart;
1214 nTime -= pProfData->nContinueTime;
1216 if ( nTime < pProfData->nMinTime )
1217 pProfData->nMinTime = nTime;
1219 if ( nTime > pProfData->nMaxTime )
1220 pProfData->nMaxTime = nTime;
1222 pProfData->nTime += nTime;
1224 pProfData->nStart = 0xFFFFFFFF;
1225 pProfData->nContinueTime = 0;
1226 pProfData->nContinueStart = 0xFFFFFFFF;
1227 break;
1229 case DBG_PROF_CONTINUE:
1230 if ( pProfData->nContinueStart != 0xFFFFFFFF )
1232 pProfData->nContinueTime += ImplGetPerfTime() - pProfData->nContinueStart;
1233 pProfData->nContinueStart = 0xFFFFFFFF;
1235 break;
1237 case DBG_PROF_PAUSE:
1238 if ( pProfData->nContinueStart == 0xFFFFFFFF )
1239 pProfData->nContinueStart = ImplGetPerfTime();
1240 break;
1244 void DbgXtor( DbgDataType* pDbgData, sal_uInt16 nAction, const void* pThis,
1245 DbgUsr fDbgUsr )
1247 DebugData* pData = ImplGetDebugData();
1249 // quick test
1250 if ( !(pData->aDbgData.nTestFlags & DBG_TEST_XTOR) )
1251 return;
1253 XtorType* pXtorData = (XtorType*)pDbgData->pData;
1254 if ( !pXtorData )
1256 pDbgData->pData = (void*)new XtorType;
1257 pXtorData = (XtorType*)pDbgData->pData;
1258 strncpy( pXtorData->aName, pDbgData->pName, DBG_MAXNAME );
1259 pXtorData->aName[DBG_MAXNAME] = '\0';
1260 pXtorData->nCtorCalls = 0;
1261 pXtorData->nDtorCalls = 0;
1262 pXtorData->nMaxCount = 0;
1263 pXtorData->nStatics = 0;
1264 pXtorData->bTest = sal_True;
1265 pData->pXtorList->Add( (void*)pXtorData );
1267 if ( !ImplDbgFilter( pData->aDbgData.aInclClassFilter, pXtorData->aName, sal_True ) )
1268 pXtorData->bTest = sal_False;
1269 if ( ImplDbgFilter( pData->aDbgData.aExclClassFilter, pXtorData->aName, sal_False ) )
1270 pXtorData->bTest = sal_False;
1272 if ( !pXtorData->bTest )
1273 return;
1275 sal_uInt16 nAct = nAction & ~DBG_XTOR_DTOROBJ;
1277 SAL_INFO_IF(
1278 ((pData->aDbgData.nTestFlags & DBG_TEST_XTOR_TRACE)
1279 && !(nAction & DBG_XTOR_DTOROBJ) && nAct != DBG_XTOR_CHKOBJ),
1280 "tools.debug",
1281 (nAct == DBG_XTOR_CTOR ? "Enter Ctor from class "
1282 : nAct == DBG_XTOR_DTOR ? "Enter Dtor from class "
1283 : "Enter method from class ") << pDbgData->pName);
1285 // If some Xtor-tests are still tracing
1286 if ( pData->aDbgData.nTestFlags & DBG_TEST_XTOR_EXTRA )
1288 // call DBG_CTOR before all other DBG_XTOR calls
1289 if ( ((nAction & ~DBG_XTOR_DTOROBJ) != DBG_XTOR_CTOR) && !pDbgData->pData )
1291 SAL_WARN(
1292 "tools.debug",
1293 "DBG_DTOR() or DBG_CHKTHIS() without DBG_CTOR(): "
1294 << pDbgData->pName);
1295 return;
1298 // Test if the pointer is still valid
1299 if ( pData->aDbgData.nTestFlags & DBG_TEST_XTOR_THIS )
1301 if ( (pData->aDbgData.nTestFlags & DBG_TEST_XTOR_EXIT) ||
1302 !(nAction & DBG_XTOR_DTOROBJ) )
1304 // This-Pointer == NULL
1305 if ( !pThis )
1307 SAL_WARN(
1308 "tools.debug",
1309 "this == NULL in class " << pDbgData->pName);
1310 return;
1313 if ( (nAction & ~DBG_XTOR_DTOROBJ) != DBG_XTOR_CTOR )
1315 SAL_WARN_IF(
1316 !pXtorData->aThisList.IsIn(pThis), "tools.debug",
1317 "invalid this-Pointer %p in class " << pDbgData->pName);
1322 // execute function test and update maintenance data
1323 const sal_Char* pMsg = NULL;
1324 switch ( nAction & ~DBG_XTOR_DTOROBJ )
1326 case DBG_XTOR_CTOR:
1327 if ( nAction & DBG_XTOR_DTOROBJ )
1329 if ( fDbgUsr &&
1330 (pData->aDbgData.nTestFlags & DBG_TEST_XTOR_EXIT) &&
1331 (pData->aDbgData.nTestFlags & DBG_TEST_XTOR_FUNC) )
1332 pMsg = fDbgUsr( pThis );
1334 else
1336 pXtorData->nCtorCalls++;
1337 if ( !bDbgImplInMain )
1338 pXtorData->nStatics++;
1339 if ( (pXtorData->nCtorCalls-pXtorData->nDtorCalls) > pXtorData->nMaxCount )
1340 pXtorData->nMaxCount = pXtorData->nCtorCalls - pXtorData->nDtorCalls;
1342 if ( pData->aDbgData.nTestFlags & DBG_TEST_XTOR_THIS )
1343 pXtorData->aThisList.Add( pThis );
1345 break;
1347 case DBG_XTOR_DTOR:
1348 if ( nAction & DBG_XTOR_DTOROBJ )
1350 pXtorData->nDtorCalls++;
1351 if ( pData->aDbgData.nTestFlags & DBG_TEST_XTOR_THIS )
1352 pXtorData->aThisList.Remove( pThis );
1354 else
1356 if ( fDbgUsr &&
1357 (pData->aDbgData.nTestFlags & DBG_TEST_XTOR_FUNC) )
1358 pMsg = fDbgUsr( pThis );
1360 break;
1362 case DBG_XTOR_CHKTHIS:
1363 case DBG_XTOR_CHKOBJ:
1364 if ( nAction & DBG_XTOR_DTOROBJ )
1366 if ( fDbgUsr &&
1367 (pData->aDbgData.nTestFlags & DBG_TEST_XTOR_EXIT) &&
1368 (pData->aDbgData.nTestFlags & DBG_TEST_XTOR_FUNC) )
1369 pMsg = fDbgUsr( pThis );
1371 else
1373 if ( fDbgUsr &&
1374 (pData->aDbgData.nTestFlags & DBG_TEST_XTOR_FUNC) )
1375 pMsg = fDbgUsr( pThis );
1377 break;
1380 SAL_WARN_IF(
1381 pMsg, "tools.debug",
1382 "Error-Msg from Object " << pThis << " in class "
1383 << pDbgData->pName << ": " << pMsg);
1386 SAL_INFO_IF(
1387 ((pData->aDbgData.nTestFlags & DBG_TEST_XTOR_TRACE)
1388 && (nAction & DBG_XTOR_DTOROBJ) && nAct != DBG_XTOR_CHKOBJ),
1389 "tools.debug",
1390 (nAct == DBG_XTOR_CTOR
1391 ? "Leave Ctor from class "
1392 : nAct == DBG_XTOR_DTOR
1393 ? "Leave Dtor from class "
1394 : "Leave method from class ") << pDbgData->pName);
1397 void DbgOut( const sal_Char* pMsg, sal_uInt16 nDbgOut, const sal_Char* pFile, sal_uInt16 nLine )
1399 static sal_Bool bIn = sal_False;
1400 if ( bIn )
1401 return;
1402 bIn = sal_True;
1404 DebugData* pData = GetDebugData();
1405 sal_Char const * pStr;
1406 sal_uIntPtr nOut;
1407 int nBufLen = 0;
1409 if ( nDbgOut == DBG_OUT_ERROR )
1411 nOut = pData->aDbgData.nErrorOut;
1412 pStr = "Error: ";
1414 else if ( nDbgOut == DBG_OUT_WARNING )
1416 nOut = pData->aDbgData.nWarningOut;
1417 pStr = "Warning: ";
1419 else
1421 nOut = pData->aDbgData.nTraceOut;
1422 pStr = NULL;
1425 if ( nOut == DBG_OUT_NULL )
1427 bIn = sal_False;
1428 return;
1431 if ( ImplDbgFilterMessage( pMsg ) )
1433 bIn = sal_False;
1434 return;
1437 ImplDbgLock();
1439 sal_Char aBufOut[DBG_BUF_MAXLEN];
1440 if ( pStr )
1442 strcpy( aBufOut, pStr );
1443 nBufLen = strlen( pStr );
1445 else
1446 aBufOut[0] = '\0';
1448 int nMsgLen = strlen( pMsg );
1449 if ( nBufLen+nMsgLen > DBG_BUF_MAXLEN )
1451 int nCopyLen = DBG_BUF_MAXLEN-nBufLen-4;
1452 strncpy( &(aBufOut[nBufLen]), pMsg, nCopyLen );
1453 strcpy( &(aBufOut[nBufLen+nCopyLen]), "..." );
1455 else
1456 strcpy( &(aBufOut[nBufLen]), pMsg );
1458 if ( pFile && nLine && (nBufLen+nMsgLen < DBG_BUF_MAXLEN) )
1460 if ( nOut == DBG_OUT_MSGBOX )
1461 strcat( aBufOut, "\n" );
1462 else
1463 strcat( aBufOut, " " );
1464 strcat( aBufOut, "From File " );
1465 strcat( aBufOut, pFile );
1466 strcat( aBufOut, " at Line " );
1468 // Convert line to String and append
1469 sal_Char aLine[9];
1470 sal_Char* pLine = &aLine[7];
1471 sal_uInt16 i;
1472 memset( aLine, 0, sizeof( aLine ) );
1475 i = nLine % 10;
1476 pLine--;
1477 *(pLine) = (sal_Char)i + 48;
1478 nLine /= 10;
1480 while ( nLine );
1481 strcat( aBufOut, pLine );
1484 if ( ( nOut >= DBG_OUT_USER_CHANNEL_0 ) && ( nOut - DBG_OUT_USER_CHANNEL_0 < pData->aDbgPrintUserChannels.size() ) )
1486 DbgPrintLine pPrinter = pData->aDbgPrintUserChannels[ nOut - DBG_OUT_USER_CHANNEL_0 ];
1487 if ( pPrinter )
1488 pPrinter( aBufOut );
1489 else
1490 nOut = DBG_OUT_DEBUGGER;
1493 if ( nOut == DBG_OUT_ABORT )
1495 if ( pData->pDbgAbort != NULL )
1496 pData->pDbgAbort( aBufOut );
1497 abort();
1500 if ( nOut == DBG_OUT_DEBUGGER )
1502 if ( !ImplActivateDebugger( aBufOut ) )
1503 nOut = DBG_OUT_TESTTOOL;
1506 if ( nOut == DBG_OUT_TESTTOOL )
1508 if ( pData->pDbgPrintTestTool )
1509 pData->pDbgPrintTestTool( aBufOut );
1510 else
1511 nOut = DBG_OUT_MSGBOX;
1514 if ( nOut == DBG_OUT_MSGBOX )
1516 if ( pData->pDbgPrintMsgBox )
1517 pData->pDbgPrintMsgBox( aBufOut );
1518 else
1519 nOut = DBG_OUT_WINDOW;
1522 if ( nOut == DBG_OUT_WINDOW )
1524 if ( pData->pDbgPrintWindow )
1525 pData->pDbgPrintWindow( aBufOut );
1526 else
1527 nOut = DBG_OUT_FILE;
1530 switch ( nOut )
1532 case DBG_OUT_SHELL:
1533 DbgPrintShell( aBufOut );
1534 break;
1535 case DBG_OUT_FILE:
1536 ImplDbgPrintFile( aBufOut );
1537 break;
1540 ImplDbgUnlock();
1542 bIn = sal_False;
1545 void DbgPrintShell(char const * message) {
1546 fprintf(stderr, "%s\n", message);
1547 #if defined WNT
1548 OutputDebugStringA(message);
1549 #endif
1552 void DbgOutTypef( sal_uInt16 nDbgOut, const sal_Char* pFStr, ... )
1554 va_list pList;
1556 va_start( pList, pFStr );
1557 sal_Char aBuf[DBG_BUF_MAXLEN];
1558 vsprintf( aBuf, pFStr, pList );
1559 va_end( pList );
1561 DbgOut( aBuf, nDbgOut );
1564 void DbgOutf( const sal_Char* pFStr, ... )
1566 va_list pList;
1568 va_start( pList, pFStr );
1569 sal_Char aBuf[DBG_BUF_MAXLEN];
1570 vsprintf( aBuf, pFStr, pList );
1571 va_end( pList );
1573 DbgOut( aBuf );
1576 #else
1578 void* DbgFunc( sal_uInt16, void* ) { return NULL; }
1580 void DbgProf( sal_uInt16, DbgDataType* ) {}
1581 void DbgXtor( DbgDataType*, sal_uInt16, const void*, DbgUsr ) {}
1583 void DbgOut( const sal_Char*, sal_uInt16, const sal_Char*, sal_uInt16 ) {}
1584 void DbgOutTypef( sal_uInt16, const sal_Char*, ... ) {}
1585 void DbgOutf( const sal_Char*, ... ) {}
1587 #endif
1590 #if OSL_DEBUG_LEVEL > 0
1592 TOOLS_DLLPUBLIC void DbgUnhandledException(const css::uno::Any & caught, const char* currentFunction, const char* fileAndLineNo)
1594 OString sMessage( "caught an exception!" );
1595 sMessage += "\nin function:";
1596 sMessage += currentFunction;
1597 sMessage += "\ntype: ";
1598 sMessage += OUStringToOString( caught.getValueTypeName(), osl_getThreadTextEncoding() );
1599 ::com::sun::star::uno::Exception exception;
1600 caught >>= exception;
1601 if ( !exception.Message.isEmpty() )
1603 sMessage += "\nmessage: ";
1604 sMessage += OUStringToOString( exception.Message, osl_getThreadTextEncoding() );
1606 if ( exception.Context.is() )
1608 const char* pContext = typeid( *exception.Context.get() ).name();
1609 sMessage += "\ncontext: ";
1610 sMessage += pContext;
1613 ::com::sun::star::configuration::CorruptedConfigurationException
1614 specialized;
1615 if ( caught >>= specialized )
1617 sMessage += "\ndetails: ";
1618 sMessage += OUStringToOString(
1619 specialized.Details, osl_getThreadTextEncoding() );
1623 ::com::sun::star::task::ErrorCodeIOException specialized;
1624 if ( caught >>= specialized )
1626 sMessage += "\ndetails: ";
1627 sMessage += OString::valueOf( specialized.ErrCode );
1630 sMessage += "\n";
1632 SAL_DETAIL_LOG_FORMAT(
1633 SAL_DETAIL_ENABLE_LOG_WARN, SAL_DETAIL_LOG_LEVEL_WARN,
1634 "legacy.osl", fileAndLineNo, "%s", sMessage.getStr());
1637 #endif // OSL_DEBUG_LEVEL
1640 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */