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