1*e1f63238SAndrew Rist /**************************************************************
2*e1f63238SAndrew Rist  *
3*e1f63238SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e1f63238SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e1f63238SAndrew Rist  * distributed with this work for additional information
6*e1f63238SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e1f63238SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e1f63238SAndrew Rist  * "License"); you may not use this file except in compliance
9*e1f63238SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*e1f63238SAndrew Rist  *
11*e1f63238SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*e1f63238SAndrew Rist  *
13*e1f63238SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e1f63238SAndrew Rist  * software distributed under the License is distributed on an
15*e1f63238SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e1f63238SAndrew Rist  * KIND, either express or implied.  See the License for the
17*e1f63238SAndrew Rist  * specific language governing permissions and limitations
18*e1f63238SAndrew Rist  * under the License.
19*e1f63238SAndrew Rist  *
20*e1f63238SAndrew Rist  *************************************************************/
21*e1f63238SAndrew Rist 
22*e1f63238SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_basic.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "rtlproto.hxx"
27cdf0e10cSrcweir #include "sbdiagnose.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "basic/sbstar.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <tools/debug.hxx>
32cdf0e10cSrcweir #include <comphelper/flagguard.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #ifdef DBG_UTIL
35cdf0e10cSrcweir 
36cdf0e10cSrcweir static DbgChannelId nRestoreChannelId = 0;
37cdf0e10cSrcweir static DbgChannelId nAssertionChannelId = 0;
38cdf0e10cSrcweir static StarBASICRef xAssertionChannelBasic;
39cdf0e10cSrcweir static String sCaptureFunctionName;
40cdf0e10cSrcweir static bool bReportingAssertion = false;
41cdf0e10cSrcweir 
ResetCapturedAssertions()42cdf0e10cSrcweir void ResetCapturedAssertions()
43cdf0e10cSrcweir {
44cdf0e10cSrcweir 	if ( nRestoreChannelId != 0 )
45cdf0e10cSrcweir 	{
46cdf0e10cSrcweir 		DBG_INSTOUTERROR( nRestoreChannelId );
47cdf0e10cSrcweir 	}
48cdf0e10cSrcweir     nRestoreChannelId = 0;
49cdf0e10cSrcweir     xAssertionChannelBasic = NULL;
50cdf0e10cSrcweir     sCaptureFunctionName = String();
51cdf0e10cSrcweir     bReportingAssertion = false;
52cdf0e10cSrcweir }
53cdf0e10cSrcweir 
DbgReportAssertion(const sal_Char * i_assertionMessage)54cdf0e10cSrcweir void DbgReportAssertion( const sal_Char* i_assertionMessage )
55cdf0e10cSrcweir {
56cdf0e10cSrcweir     if ( !xAssertionChannelBasic )
57cdf0e10cSrcweir     {
58cdf0e10cSrcweir         ResetCapturedAssertions();
59cdf0e10cSrcweir         return;
60cdf0e10cSrcweir     }
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     // prevent infinite recursion
63cdf0e10cSrcweir     if ( bReportingAssertion )
64cdf0e10cSrcweir         return;
65cdf0e10cSrcweir     ::comphelper::FlagRestorationGuard aGuard( bReportingAssertion, true );
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 	SbxArrayRef const xArguments( new SbxArray( SbxVARIANT ) );
68cdf0e10cSrcweir 	SbxVariableRef const xMessageText = new SbxVariable( SbxSTRING );
69cdf0e10cSrcweir     xMessageText->PutString( String::CreateFromAscii( i_assertionMessage ) );
70cdf0e10cSrcweir     xArguments->Put( xMessageText, 1 );
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     ErrCode const nError = xAssertionChannelBasic->Call( sCaptureFunctionName, xArguments );
73cdf0e10cSrcweir     if ( ( nError & SbERR_METHOD_NOT_FOUND ) != 0 )
74cdf0e10cSrcweir         ResetCapturedAssertions();
75cdf0e10cSrcweir }
76cdf0e10cSrcweir 
77cdf0e10cSrcweir #endif
78cdf0e10cSrcweir 
79cdf0e10cSrcweir /// capture assertions, route them to the given given Basic function
RTLFUNC(CaptureAssertions)80cdf0e10cSrcweir RTLFUNC(CaptureAssertions)
81cdf0e10cSrcweir {
82cdf0e10cSrcweir     (void)bWrite;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     // need exactly one argument
85cdf0e10cSrcweir 	if ( rPar.Count() != 2 )
86cdf0e10cSrcweir 	{
87cdf0e10cSrcweir 		StarBASIC::Error( SbERR_BAD_ARGUMENT );
88cdf0e10cSrcweir 		return;
89cdf0e10cSrcweir 	}
90cdf0e10cSrcweir 
91cdf0e10cSrcweir #ifdef DBG_UTIL
92cdf0e10cSrcweir     DBG_TESTSOLARMUTEX();
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     String const sFunctionName = rPar.Get(1)->GetString();
95cdf0e10cSrcweir     if ( sFunctionName.Len() == 0 )
96cdf0e10cSrcweir     {
97cdf0e10cSrcweir         ResetCapturedAssertions();
98cdf0e10cSrcweir         return;
99cdf0e10cSrcweir     }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     if ( nAssertionChannelId == 0 )
102cdf0e10cSrcweir     {
103cdf0e10cSrcweir         // TODO: should we register a named channel at the VCL API, instead of an unnamed channel at the tools API?
104cdf0e10cSrcweir         // A named channel would mean it would appear in the nonpro-debug-options dialog
105cdf0e10cSrcweir         nAssertionChannelId = DbgRegisterUserChannel( &DbgReportAssertion );
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     DbgChannelId const nCurrentChannelId = (DbgChannelId)DbgGetErrorOut();
109cdf0e10cSrcweir     if ( nCurrentChannelId != nAssertionChannelId )
110cdf0e10cSrcweir     {
111cdf0e10cSrcweir         // remember the current channel
112cdf0e10cSrcweir         nRestoreChannelId = nCurrentChannelId;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         // set the new channel
115cdf0e10cSrcweir         DBG_INSTOUTERROR( nAssertionChannelId );
116cdf0e10cSrcweir 
117cdf0e10cSrcweir         // ensure OSL assertions are captured, too
118cdf0e10cSrcweir         DbgData aData( *DbgGetData() );
119cdf0e10cSrcweir         aData.bHookOSLAssert = sal_True;
120cdf0e10cSrcweir         DbgUpdateOslHook( &aData );
121cdf0e10cSrcweir     }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     xAssertionChannelBasic = pBasic;
124cdf0e10cSrcweir     sCaptureFunctionName = sFunctionName;
125cdf0e10cSrcweir #else
126cdf0e10cSrcweir     (void)pBasic;
127cdf0e10cSrcweir     (void)rPar;
128cdf0e10cSrcweir     (void)bWrite;
129cdf0e10cSrcweir #endif
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132