xref: /trunk/main/sw/source/core/except/dbgloop.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*efeef26fSAndrew Rist  * distributed with this work for additional information
6*efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9*efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15*efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*efeef26fSAndrew Rist  * specific language governing permissions and limitations
18*efeef26fSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*efeef26fSAndrew Rist  *************************************************************/
21*efeef26fSAndrew Rist 
22*efeef26fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sw.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #ifdef DBG_UTIL
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <tools/stream.hxx>
30cdf0e10cSrcweir #include "dbgloop.hxx"
31cdf0e10cSrcweir #include "errhdl.hxx"
32cdf0e10cSrcweir 
33cdf0e10cSrcweir DbgLoopStack DbgLoop::aDbgLoopStack;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir /*************************************************************************
36cdf0e10cSrcweir  *                      class DbgLoopStack
37cdf0e10cSrcweir  *************************************************************************/
38cdf0e10cSrcweir 
DbgLoopStack()39cdf0e10cSrcweir DbgLoopStack::DbgLoopStack()
40cdf0e10cSrcweir {
41cdf0e10cSrcweir     Reset();
42cdf0e10cSrcweir }
43cdf0e10cSrcweir 
Reset()44cdf0e10cSrcweir void DbgLoopStack::Reset()
45cdf0e10cSrcweir {
46cdf0e10cSrcweir     nPtr = 0;
47cdf0e10cSrcweir     pDbg = 0;
48cdf0e10cSrcweir     for( sal_uInt16 i = 0; i < DBG_MAX_STACK; ++i )
49cdf0e10cSrcweir         aCount[i] = 0;
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
52cdf0e10cSrcweir /*************************************************************************
53cdf0e10cSrcweir  *                       DbgLoopStack::Push()
54cdf0e10cSrcweir  *************************************************************************/
55cdf0e10cSrcweir 
Push(const void * pThis)56cdf0e10cSrcweir void DbgLoopStack::Push( const void *pThis )
57cdf0e10cSrcweir {
58cdf0e10cSrcweir     // Wir muessen irgendwie mitbekommen, wann die erste Stackposition
59cdf0e10cSrcweir     // resettet werden soll, z.B. wenn wir einen Nullpointer uebergeben
60cdf0e10cSrcweir     if( !nPtr && ( pDbg != pThis || !pThis ) )
61cdf0e10cSrcweir     {
62cdf0e10cSrcweir         aCount[1] = 0;
63cdf0e10cSrcweir         pDbg = pThis;
64cdf0e10cSrcweir     }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     ++nPtr;
67cdf0e10cSrcweir     if( DBG_MAX_STACK > nPtr )
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir         // Wenn eine loop entdeckt wird, wird der counter wieder zurueckgesetzt.
70cdf0e10cSrcweir         ASSERT( DBG_MAX_LOOP > aCount[nPtr], "DbgLoopStack::Push: loop detected" );
71cdf0e10cSrcweir         if( DBG_MAX_LOOP > aCount[nPtr] )
72cdf0e10cSrcweir             ++(aCount[nPtr]);
73cdf0e10cSrcweir         else
74cdf0e10cSrcweir             aCount[nPtr] = 0;
75cdf0e10cSrcweir     }
76cdf0e10cSrcweir }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir /*************************************************************************
79cdf0e10cSrcweir  *                       DbgLoopStack::Pop()
80cdf0e10cSrcweir  *************************************************************************/
81cdf0e10cSrcweir 
Pop()82cdf0e10cSrcweir void DbgLoopStack::Pop()
83cdf0e10cSrcweir {
84cdf0e10cSrcweir     if( DBG_MAX_STACK > nPtr )
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir         ASSERT( nPtr, "DbgLoopStack::Pop: can't pop the stack" );
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         ASSERT( aCount[nPtr], "DbgLoopStack::Pop: can't dec the count" );
89cdf0e10cSrcweir         if( DBG_MAX_STACK > nPtr + 1 )
90cdf0e10cSrcweir             aCount[nPtr + 1] = 0;
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir     --nPtr;
93cdf0e10cSrcweir }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir /*************************************************************************
96cdf0e10cSrcweir  *                       DbgLoopStack::Print()
97cdf0e10cSrcweir  *************************************************************************/
98cdf0e10cSrcweir 
Print(SvStream & rOS) const99cdf0e10cSrcweir void DbgLoopStack::Print( SvStream &rOS ) const
100cdf0e10cSrcweir {
101cdf0e10cSrcweir     rOS << "POS: " << nPtr << '\n';
102cdf0e10cSrcweir     sal_uInt16 i;
103cdf0e10cSrcweir     for( i = 0; i < DBG_MAX_STACK; ++i )
104cdf0e10cSrcweir         rOS << i << " ";
105cdf0e10cSrcweir     rOS << '\n';
106cdf0e10cSrcweir     for( i = 0; i < DBG_MAX_STACK; ++i )
107cdf0e10cSrcweir         rOS << aCount[i] << " ";
108cdf0e10cSrcweir     rOS << '\n';
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir #ifdef STAND_ALONE
112cdf0e10cSrcweir // compile with: cl /AL /DSTAND_ALONE dbgloop.cxx
113cdf0e10cSrcweir 
114cdf0e10cSrcweir /*************************************************************************
115cdf0e10cSrcweir  *                          main()
116cdf0e10cSrcweir  *************************************************************************/
117cdf0e10cSrcweir 
118cdf0e10cSrcweir #include <stdlib.h>
119cdf0e10cSrcweir 
AssertFail(const char * pErr,const char * pFile,sal_uInt16 nLine)120cdf0e10cSrcweir void AssertFail( const char *pErr, const char *pFile, sal_uInt16 nLine )
121cdf0e10cSrcweir {
122cdf0e10cSrcweir     cout << pErr << '\n';
123cdf0e10cSrcweir     PrintLoopStack( cout );
124cdf0e10cSrcweir     exit(0);
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir class Test
128cdf0e10cSrcweir {
129cdf0e10cSrcweir public:
130cdf0e10cSrcweir         void Run() const;
131cdf0e10cSrcweir };
132cdf0e10cSrcweir 
Run() const133cdf0e10cSrcweir void Test::Run() const
134cdf0e10cSrcweir {
135cdf0e10cSrcweir     cout << "---" << '\n';
136cdf0e10cSrcweir     for( sal_uInt16 i = 0; i < 10; ++i )
137cdf0e10cSrcweir     {
138cdf0e10cSrcweir         cout << "i" << i;
139cdf0e10cSrcweir         DBG_LOOP;
140cdf0e10cSrcweir         PrintLoopStack( cout );
141cdf0e10cSrcweir         for( sal_uInt16 j = 0; j < 10; ++j )
142cdf0e10cSrcweir         {
143cdf0e10cSrcweir             cout << " j" << j;
144cdf0e10cSrcweir             DBG_LOOP;
145cdf0e10cSrcweir             PrintLoopStack( cout );
146cdf0e10cSrcweir         }
147cdf0e10cSrcweir         cout << '\n';
148cdf0e10cSrcweir     }
149cdf0e10cSrcweir     PrintLoopStack( cout );
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
main()152cdf0e10cSrcweir int main()
153cdf0e10cSrcweir {
154cdf0e10cSrcweir     // unterschiedliche Instanzen waehlen wg. pDbg != pThis
155cdf0e10cSrcweir     Test aTest1;
156cdf0e10cSrcweir     aTest1.Run();
157cdf0e10cSrcweir     Test aTest2;
158cdf0e10cSrcweir     aTest2.Run();
159cdf0e10cSrcweir     return 0;
160cdf0e10cSrcweir }
161cdf0e10cSrcweir #endif
162cdf0e10cSrcweir 
163cdf0e10cSrcweir #endif // DBG_UTIL
164