1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3*cdf0e10cSrcweir  *
4*cdf0e10cSrcweir  * Copyright 2000, 2011 Oracle and/or its affiliates.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
7*cdf0e10cSrcweir  *
8*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
9*cdf0e10cSrcweir  *
10*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
11*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
12*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
13*cdf0e10cSrcweir  *
14*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
15*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
18*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
19*cdf0e10cSrcweir  *
20*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
21*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
22*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
23*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
24*cdf0e10cSrcweir  *
25*cdf0e10cSrcweir  ************************************************************************/
26*cdf0e10cSrcweir 
27*cdf0e10cSrcweir #include "precompiled_basic.hxx"
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #include "rtlproto.hxx"
30*cdf0e10cSrcweir #include "sbdiagnose.hxx"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include "basic/sbstar.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <tools/debug.hxx>
35*cdf0e10cSrcweir #include <comphelper/flagguard.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #ifdef DBG_UTIL
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir static DbgChannelId nRestoreChannelId = 0;
40*cdf0e10cSrcweir static DbgChannelId nAssertionChannelId = 0;
41*cdf0e10cSrcweir static StarBASICRef xAssertionChannelBasic;
42*cdf0e10cSrcweir static String sCaptureFunctionName;
43*cdf0e10cSrcweir static bool bReportingAssertion = false;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir void ResetCapturedAssertions()
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir 	if ( nRestoreChannelId != 0 )
48*cdf0e10cSrcweir 	{
49*cdf0e10cSrcweir 		DBG_INSTOUTERROR( nRestoreChannelId );
50*cdf0e10cSrcweir 	}
51*cdf0e10cSrcweir     nRestoreChannelId = 0;
52*cdf0e10cSrcweir     xAssertionChannelBasic = NULL;
53*cdf0e10cSrcweir     sCaptureFunctionName = String();
54*cdf0e10cSrcweir     bReportingAssertion = false;
55*cdf0e10cSrcweir }
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir void DbgReportAssertion( const sal_Char* i_assertionMessage )
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir     if ( !xAssertionChannelBasic )
60*cdf0e10cSrcweir     {
61*cdf0e10cSrcweir         ResetCapturedAssertions();
62*cdf0e10cSrcweir         return;
63*cdf0e10cSrcweir     }
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir     // prevent infinite recursion
66*cdf0e10cSrcweir     if ( bReportingAssertion )
67*cdf0e10cSrcweir         return;
68*cdf0e10cSrcweir     ::comphelper::FlagRestorationGuard aGuard( bReportingAssertion, true );
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 	SbxArrayRef const xArguments( new SbxArray( SbxVARIANT ) );
71*cdf0e10cSrcweir 	SbxVariableRef const xMessageText = new SbxVariable( SbxSTRING );
72*cdf0e10cSrcweir     xMessageText->PutString( String::CreateFromAscii( i_assertionMessage ) );
73*cdf0e10cSrcweir     xArguments->Put( xMessageText, 1 );
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir     ErrCode const nError = xAssertionChannelBasic->Call( sCaptureFunctionName, xArguments );
76*cdf0e10cSrcweir     if ( ( nError & SbERR_METHOD_NOT_FOUND ) != 0 )
77*cdf0e10cSrcweir         ResetCapturedAssertions();
78*cdf0e10cSrcweir }
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir #endif
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir /// capture assertions, route them to the given given Basic function
83*cdf0e10cSrcweir RTLFUNC(CaptureAssertions)
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir     (void)bWrite;
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir     // need exactly one argument
88*cdf0e10cSrcweir 	if ( rPar.Count() != 2 )
89*cdf0e10cSrcweir 	{
90*cdf0e10cSrcweir 		StarBASIC::Error( SbERR_BAD_ARGUMENT );
91*cdf0e10cSrcweir 		return;
92*cdf0e10cSrcweir 	}
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir #ifdef DBG_UTIL
95*cdf0e10cSrcweir     DBG_TESTSOLARMUTEX();
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir     String const sFunctionName = rPar.Get(1)->GetString();
98*cdf0e10cSrcweir     if ( sFunctionName.Len() == 0 )
99*cdf0e10cSrcweir     {
100*cdf0e10cSrcweir         ResetCapturedAssertions();
101*cdf0e10cSrcweir         return;
102*cdf0e10cSrcweir     }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     if ( nAssertionChannelId == 0 )
105*cdf0e10cSrcweir     {
106*cdf0e10cSrcweir         // TODO: should we register a named channel at the VCL API, instead of an unnamed channel at the tools API?
107*cdf0e10cSrcweir         // A named channel would mean it would appear in the nonpro-debug-options dialog
108*cdf0e10cSrcweir         nAssertionChannelId = DbgRegisterUserChannel( &DbgReportAssertion );
109*cdf0e10cSrcweir     }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     DbgChannelId const nCurrentChannelId = (DbgChannelId)DbgGetErrorOut();
112*cdf0e10cSrcweir     if ( nCurrentChannelId != nAssertionChannelId )
113*cdf0e10cSrcweir     {
114*cdf0e10cSrcweir         // remember the current channel
115*cdf0e10cSrcweir         nRestoreChannelId = nCurrentChannelId;
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir         // set the new channel
118*cdf0e10cSrcweir         DBG_INSTOUTERROR( nAssertionChannelId );
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir         // ensure OSL assertions are captured, too
121*cdf0e10cSrcweir         DbgData aData( *DbgGetData() );
122*cdf0e10cSrcweir         aData.bHookOSLAssert = sal_True;
123*cdf0e10cSrcweir         DbgUpdateOslHook( &aData );
124*cdf0e10cSrcweir     }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     xAssertionChannelBasic = pBasic;
127*cdf0e10cSrcweir     sCaptureFunctionName = sFunctionName;
128*cdf0e10cSrcweir #else
129*cdf0e10cSrcweir     (void)pBasic;
130*cdf0e10cSrcweir     (void)rPar;
131*cdf0e10cSrcweir     (void)bWrite;
132*cdf0e10cSrcweir #endif
133*cdf0e10cSrcweir }
134*cdf0e10cSrcweir 
135