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