xref: /trunk/main/tools/source/debug/stcktree.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*89b56da7SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*89b56da7SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*89b56da7SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*89b56da7SAndrew Rist  * distributed with this work for additional information
6*89b56da7SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*89b56da7SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*89b56da7SAndrew Rist  * "License"); you may not use this file except in compliance
9*89b56da7SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*89b56da7SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*89b56da7SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*89b56da7SAndrew Rist  * software distributed under the License is distributed on an
15*89b56da7SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*89b56da7SAndrew Rist  * KIND, either express or implied.  See the License for the
17*89b56da7SAndrew Rist  * specific language governing permissions and limitations
18*89b56da7SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*89b56da7SAndrew Rist  *************************************************************/
21*89b56da7SAndrew Rist 
22*89b56da7SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_tools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <string.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <tools/debug.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir // -----------------------------------------------------------------------
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #if defined( DBG_UTIL ) && defined( WNT ) && defined( INTEL )
34cdf0e10cSrcweir 
35cdf0e10cSrcweir struct ImpDbgStackTree
36cdf0e10cSrcweir {
37cdf0e10cSrcweir     ImpDbgStackTree*    pLeft_;
38cdf0e10cSrcweir     ImpDbgStackTree*    pRight_;
39cdf0e10cSrcweir     ImpDbgStackTree*    pCaller_;
40cdf0e10cSrcweir     ImpDbgStackTree*    pSub_;
41cdf0e10cSrcweir     sal_uIntPtr             nIP_;
42cdf0e10cSrcweir     sal_uIntPtr             nBytesLeak_;
43cdf0e10cSrcweir     sal_uIntPtr             nBytesPeak_;
44cdf0e10cSrcweir     sal_uIntPtr             nBytes_;
45cdf0e10cSrcweir     sal_uIntPtr             nCountLeak_;
46cdf0e10cSrcweir     sal_uIntPtr             nCountPeak_;
47cdf0e10cSrcweir     sal_uIntPtr             nCount_;
48cdf0e10cSrcweir     sal_uIntPtr             nMax_;
49cdf0e10cSrcweir     sal_uIntPtr             nMin_;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir                         ImpDbgStackTree( ImpDbgStackTree* pSub, sal_uIntPtr nIP );
52cdf0e10cSrcweir                         ~ImpDbgStackTree();
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     ImpDbgStackTree*    Add( sal_uIntPtr nAlloc, sal_uIntPtr* pBP, sal_uIntPtr nIP );
55cdf0e10cSrcweir     void                Print( int nLevel, sal_uIntPtr nCount, sal_uIntPtr nCountLeak );
56cdf0e10cSrcweir     void                Print( int nLevel );
57cdf0e10cSrcweir };
58cdf0e10cSrcweir 
59cdf0e10cSrcweir static ImpDbgStackTree* pImpDbgStackTreeRoot     = NULL;
60cdf0e10cSrcweir static sal_uIntPtr*         pImpDbgStackTreeBP       = NULL;
61cdf0e10cSrcweir static sal_uIntPtr          nImpDbgStackTreeMain     = 0;
62cdf0e10cSrcweir static int              nImpDbgStackTreeSem      = 0;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir // -----------------------------------------------------------------------
65cdf0e10cSrcweir 
ImpDbgStackTree(ImpDbgStackTree * pSub,sal_uIntPtr nIP)66cdf0e10cSrcweir ImpDbgStackTree::ImpDbgStackTree( ImpDbgStackTree* pSub, sal_uIntPtr nIP )
67cdf0e10cSrcweir {
68cdf0e10cSrcweir     pSub_ = pSub;
69cdf0e10cSrcweir     nIP_ = nIP;
70cdf0e10cSrcweir     pLeft_ = pRight_ = pCaller_ = NULL;
71cdf0e10cSrcweir     nBytesLeak_ = nBytesPeak_ = nBytes_ = 0;
72cdf0e10cSrcweir     nCountLeak_ = nCountPeak_ = nCount_ = 0;
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir // -----------------------------------------------------------------------
76cdf0e10cSrcweir 
~ImpDbgStackTree()77cdf0e10cSrcweir ImpDbgStackTree::~ImpDbgStackTree()
78cdf0e10cSrcweir {
79cdf0e10cSrcweir     if ( pLeft_ )
80cdf0e10cSrcweir         delete pLeft_;
81cdf0e10cSrcweir     if ( pRight_ )
82cdf0e10cSrcweir         delete pRight_;
83cdf0e10cSrcweir     if ( pCaller_ )
84cdf0e10cSrcweir         delete pCaller_;
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir // -----------------------------------------------------------------------
88cdf0e10cSrcweir 
Print(int nLevel,sal_uIntPtr nCount,sal_uIntPtr nCountLeak)89cdf0e10cSrcweir void ImpDbgStackTree::Print( int nLevel, sal_uIntPtr nCount, sal_uIntPtr nCountLeak )
90cdf0e10cSrcweir {
91cdf0e10cSrcweir     if ( pLeft_ )
92cdf0e10cSrcweir         pLeft_->Print( nLevel, nCount, nCountLeak );
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     if ( nCount_ >= nCount && nCountLeak_ >= nCountLeak )
95cdf0e10cSrcweir     {
96cdf0e10cSrcweir         if ( nMax_ == nMin_ )
97cdf0e10cSrcweir         {
98cdf0e10cSrcweir             sal_uIntPtr nTemp = nCountLeak_ * nMin_;
99cdf0e10cSrcweir             DbgOutf( "%*c%08lx Count=%lu/%lu/%lu Bytes=%lu/%lu/%lu Size=%lu",
100cdf0e10cSrcweir                      nLevel, ' ', nIP_,
101cdf0e10cSrcweir                      nCount_, nCountPeak_, nCountLeak_,
102cdf0e10cSrcweir                      nBytes_, nBytesPeak_, nTemp,
103cdf0e10cSrcweir                      nMin_ );
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir         else
106cdf0e10cSrcweir         {
107cdf0e10cSrcweir             DbgOutf( "%*c%08lx Count=%lu/%lu/%lu Bytes=%lu/%lu/%lu Size=%lu-%lu",
108cdf0e10cSrcweir                      nLevel, ' ', nIP_,
109cdf0e10cSrcweir                      nCount_, nCountPeak_, nCountLeak_,
110cdf0e10cSrcweir                      nBytes_, nBytesPeak_, nBytesLeak_,
111cdf0e10cSrcweir                      nMin_, nMax_ );
112cdf0e10cSrcweir         }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         if ( pCaller_ )
115cdf0e10cSrcweir             if( nLevel > 3 && nCountLeak )
116cdf0e10cSrcweir                 pCaller_->Print( nLevel + 1, nCount, 1 );
117cdf0e10cSrcweir             else
118cdf0e10cSrcweir                 pCaller_->Print( nLevel + 1, nCount, nCountLeak );
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     if ( pRight_ )
122cdf0e10cSrcweir         pRight_->Print( nLevel, nCount, nCountLeak );
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir // -----------------------------------------------------------------------
126cdf0e10cSrcweir 
Print(int nLevel)127cdf0e10cSrcweir void ImpDbgStackTree::Print( int nLevel )
128cdf0e10cSrcweir {
129cdf0e10cSrcweir     if ( pSub_ )
130cdf0e10cSrcweir         pSub_->Print( nLevel + 1 );
131cdf0e10cSrcweir     DbgOutf( "%*c%08lx", nLevel, ' ',nIP_ );
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir // -----------------------------------------------------------------------
135cdf0e10cSrcweir 
Add(sal_uIntPtr nAlloc,sal_uIntPtr * pBP,sal_uIntPtr nIP)136cdf0e10cSrcweir ImpDbgStackTree* ImpDbgStackTree::Add( sal_uIntPtr nAlloc, sal_uIntPtr *pBP, sal_uIntPtr nIP )
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     if ( nIP < nIP_ )
139cdf0e10cSrcweir     {
140cdf0e10cSrcweir         if ( !pLeft_ )
141cdf0e10cSrcweir             pLeft_ = new ImpDbgStackTree( pSub_, nIP );
142cdf0e10cSrcweir         return pLeft_->Add( nAlloc, pBP, nIP );
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir     if ( nIP > nIP_ )
145cdf0e10cSrcweir     {
146cdf0e10cSrcweir         if ( !pRight_ )
147cdf0e10cSrcweir             pRight_ = new ImpDbgStackTree( pSub_, nIP );
148cdf0e10cSrcweir         return pRight_->Add( nAlloc, pBP, nIP );
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     nCount_++;
152cdf0e10cSrcweir     nCountLeak_++;
153cdf0e10cSrcweir     if ( nCountLeak_ > nCountPeak_ )
154cdf0e10cSrcweir         nCountPeak_ = nCountLeak_;
155cdf0e10cSrcweir     nBytes_     += nAlloc;
156cdf0e10cSrcweir     nBytesLeak_ += nAlloc;
157cdf0e10cSrcweir     if ( nBytesLeak_ > nBytesPeak_ )
158cdf0e10cSrcweir         nBytesPeak_ = nBytesLeak_;
159cdf0e10cSrcweir     if ( nCount_ == 1 )
160cdf0e10cSrcweir         nMax_ = nMin_ = nAlloc;
161cdf0e10cSrcweir     else if ( nMax_ < nAlloc )
162cdf0e10cSrcweir         nMax_ = nAlloc;
163cdf0e10cSrcweir     else if ( nMin_ > nAlloc )
164cdf0e10cSrcweir         nMin_ = nAlloc;
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     if ( !(pBP[0] & 3) && (sal_uIntPtr)pBP < pBP[0] && pBP[0] < (sal_uIntPtr)pImpDbgStackTreeBP )
167cdf0e10cSrcweir     {
168cdf0e10cSrcweir         pBP = (sal_uIntPtr*)pBP[0];
169cdf0e10cSrcweir         nIP = pBP[1];
170cdf0e10cSrcweir         if ( 0x01100000 <= nIP && nIP < 0x20000000 && nIP != nImpDbgStackTreeMain )
171cdf0e10cSrcweir         {
172cdf0e10cSrcweir             if ( !pCaller_ )
173cdf0e10cSrcweir                 pCaller_ = new ImpDbgStackTree( this, nIP );
174cdf0e10cSrcweir             return pCaller_->Add( nAlloc, pBP, nIP );
175cdf0e10cSrcweir         }
176cdf0e10cSrcweir         else
177cdf0e10cSrcweir             return this;
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     return this;
181cdf0e10cSrcweir }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir // -----------------------------------------------------------------------
184cdf0e10cSrcweir 
DbgStartStackTree()185cdf0e10cSrcweir void DbgStartStackTree()
186cdf0e10cSrcweir {
187cdf0e10cSrcweir     if ( !nImpDbgStackTreeMain )
188cdf0e10cSrcweir     {
189cdf0e10cSrcweir         sal_uIntPtr* pBP;
190cdf0e10cSrcweir         __asm mov pBP, ebp;
191cdf0e10cSrcweir 
192cdf0e10cSrcweir         pImpDbgStackTreeBP   = (sal_uIntPtr*)pBP[0];
193cdf0e10cSrcweir         nImpDbgStackTreeMain = pImpDbgStackTreeBP[1];
194cdf0e10cSrcweir     }
195cdf0e10cSrcweir }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir // -----------------------------------------------------------------------
198cdf0e10cSrcweir 
DbgEndStackTree()199cdf0e10cSrcweir void DbgEndStackTree()
200cdf0e10cSrcweir {
201cdf0e10cSrcweir     if ( nImpDbgStackTreeMain )
202cdf0e10cSrcweir     {
203cdf0e10cSrcweir         nImpDbgStackTreeMain = 0;
204cdf0e10cSrcweir         if ( pImpDbgStackTreeRoot )
205cdf0e10cSrcweir         {
206cdf0e10cSrcweir             // Ausgaben ins File umleiten
207cdf0e10cSrcweir             DbgData* pData = DbgGetData();
208cdf0e10cSrcweir             sal_uIntPtr nOldOut = pData->nTraceOut;
209cdf0e10cSrcweir             pData->nTraceOut = DBG_OUT_FILE;
210cdf0e10cSrcweir 
211cdf0e10cSrcweir             DbgOutf( "Leak-Report" );
212cdf0e10cSrcweir             DbgOutf( "===========" );
213cdf0e10cSrcweir             DbgOutf( "Mem-StackTree:" );
214cdf0e10cSrcweir             DbgOutf( "{" );
215cdf0e10cSrcweir             pImpDbgStackTreeRoot->Print( 1, 1, 2 );
216cdf0e10cSrcweir             DbgOutf( "}" );
217cdf0e10cSrcweir 
218cdf0e10cSrcweir             DbgOutf( "Alloc-Report" );
219cdf0e10cSrcweir             DbgOutf( "===========" );
220cdf0e10cSrcweir             DbgOutf( "Mem-StackTree:" );
221cdf0e10cSrcweir             DbgOutf( "{" );
222cdf0e10cSrcweir             pImpDbgStackTreeRoot->Print( 1, 1000, 0 ); // ???
223cdf0e10cSrcweir             DbgOutf( "}" );
224cdf0e10cSrcweir 
225cdf0e10cSrcweir             pData->nTraceOut = nOldOut;
226cdf0e10cSrcweir 
227cdf0e10cSrcweir             nImpDbgStackTreeSem++;
228cdf0e10cSrcweir             delete pImpDbgStackTreeRoot;
229cdf0e10cSrcweir             pImpDbgStackTreeRoot = NULL;
230cdf0e10cSrcweir             nImpDbgStackTreeSem--;
231cdf0e10cSrcweir         }
232cdf0e10cSrcweir     }
233cdf0e10cSrcweir }
234cdf0e10cSrcweir 
235cdf0e10cSrcweir // -----------------------------------------------------------------------
236cdf0e10cSrcweir 
DbgGetStackTree(sal_uIntPtr nAlloc)237cdf0e10cSrcweir void* DbgGetStackTree( sal_uIntPtr nAlloc )
238cdf0e10cSrcweir {
239cdf0e10cSrcweir     ImpDbgStackTree* pReturn = NULL;
240cdf0e10cSrcweir 
241cdf0e10cSrcweir     if ( nImpDbgStackTreeMain && !nImpDbgStackTreeSem )
242cdf0e10cSrcweir     {
243cdf0e10cSrcweir         nImpDbgStackTreeSem++;
244cdf0e10cSrcweir 
245cdf0e10cSrcweir         sal_uIntPtr* pBP;
246cdf0e10cSrcweir         __asm mov pBP, ebp;
247cdf0e10cSrcweir 
248cdf0e10cSrcweir         sal_uIntPtr  nIP = pBP[1];
249cdf0e10cSrcweir         if ( !pImpDbgStackTreeRoot )
250cdf0e10cSrcweir             pImpDbgStackTreeRoot = new ImpDbgStackTree( NULL, nIP );
251cdf0e10cSrcweir         pReturn = pImpDbgStackTreeRoot->Add( nAlloc, pBP, nIP );
252cdf0e10cSrcweir         nImpDbgStackTreeSem--;
253cdf0e10cSrcweir     }
254cdf0e10cSrcweir 
255cdf0e10cSrcweir     return pReturn;
256cdf0e10cSrcweir }
257cdf0e10cSrcweir 
258cdf0e10cSrcweir // -----------------------------------------------------------------------
259cdf0e10cSrcweir 
DbgFreeStackTree(void * pVoid,sal_uIntPtr nAlloc)260cdf0e10cSrcweir void DbgFreeStackTree( void* pVoid, sal_uIntPtr nAlloc )
261cdf0e10cSrcweir {
262cdf0e10cSrcweir     ImpDbgStackTree* p = (ImpDbgStackTree*)pVoid;
263cdf0e10cSrcweir 
264cdf0e10cSrcweir     if ( p && nImpDbgStackTreeMain && !nImpDbgStackTreeSem )
265cdf0e10cSrcweir     {
266cdf0e10cSrcweir         if ( nAlloc < p->nMin_ )
267cdf0e10cSrcweir             nAlloc = p->nMin_;
268cdf0e10cSrcweir 
269cdf0e10cSrcweir         p->nCountLeak_--;
270cdf0e10cSrcweir         p->nBytesLeak_ -= nAlloc;
271cdf0e10cSrcweir 
272cdf0e10cSrcweir         if ( p->nMax_ && 0xFFFFFFFF / p->nMax_ > p->nCountLeak_ )
273cdf0e10cSrcweir         {
274cdf0e10cSrcweir             if ( p->nBytesLeak_ > p->nMax_ * p->nCountLeak_ )
275cdf0e10cSrcweir             {
276cdf0e10cSrcweir                 nAlloc         += p->nBytesLeak_ - p->nMax_ * p->nCountLeak_;
277cdf0e10cSrcweir                 p->nBytesLeak_  = p->nMax_ * p->nCountLeak_;
278cdf0e10cSrcweir             }
279cdf0e10cSrcweir         }
280cdf0e10cSrcweir 
281cdf0e10cSrcweir         if ( p->pSub_ )
282cdf0e10cSrcweir             DbgFreeStackTree( (void*)(p->pSub_), nAlloc );
283cdf0e10cSrcweir     }
284cdf0e10cSrcweir }
285cdf0e10cSrcweir 
286cdf0e10cSrcweir // -----------------------------------------------------------------------
287cdf0e10cSrcweir 
DbgPrintStackTree(void * pVoid)288cdf0e10cSrcweir void DbgPrintStackTree( void* pVoid )
289cdf0e10cSrcweir {
290cdf0e10cSrcweir     ImpDbgStackTree* p = (ImpDbgStackTree*)pVoid;
291cdf0e10cSrcweir 
292cdf0e10cSrcweir     if ( p && nImpDbgStackTreeMain && !nImpDbgStackTreeSem )
293cdf0e10cSrcweir     {
294cdf0e10cSrcweir         // Ausgaben ins File umleiten
295cdf0e10cSrcweir         DbgData* pData = DbgGetData();
296cdf0e10cSrcweir         sal_uIntPtr nOldOut = pData->nTraceOut;
297cdf0e10cSrcweir         pData->nTraceOut = DBG_OUT_FILE;
298cdf0e10cSrcweir 
299cdf0e10cSrcweir         DbgOutf( "Mem-StackTree:" );
300cdf0e10cSrcweir         DbgOutf( "{" );
301cdf0e10cSrcweir         p->Print( 1 );
302cdf0e10cSrcweir         DbgOutf( "}" );
303cdf0e10cSrcweir 
304cdf0e10cSrcweir         pData->nTraceOut = nOldOut;
305cdf0e10cSrcweir     }
306cdf0e10cSrcweir }
307cdf0e10cSrcweir 
308cdf0e10cSrcweir #else
309cdf0e10cSrcweir 
DbgStartStackTree()310cdf0e10cSrcweir void DbgStartStackTree() {}
DbgEndStackTree()311cdf0e10cSrcweir void DbgEndStackTree() {}
DbgGetStackTree(sal_uIntPtr)312cdf0e10cSrcweir void* DbgGetStackTree( sal_uIntPtr ) { return NULL; }
DbgFreeStackTree(void *,sal_uIntPtr)313cdf0e10cSrcweir void DbgFreeStackTree( void*, sal_uIntPtr ) {}
DbgPrintStackTree(void *)314cdf0e10cSrcweir void DbgPrintStackTree( void* ) {}
315cdf0e10cSrcweir 
316cdf0e10cSrcweir #endif
317