summaryrefslogtreecommitdiff
path: root/basic/source/comp/sbcomp.cxx
blob: b3ee0a18f5ecff2bd379431fc74622e852d0fddd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */


#include <basic/sbx.hxx>
#include "sbcomp.hxx"
#include "image.hxx"
#include <basic/sbobjmod.hxx>
#include <stdio.h>

// To activate tracing enable in sbtrace.hxx
#ifdef DBG_TRACE_BASIC

// Trace ini file (set NULL to ignore)
// can be overridden with the environment variable OOO_BASICTRACEINI
static char     GpTraceIniFile[] = "~/BasicTrace.ini";
//static char*  GpTraceIniFile = NULL;

// Trace Settings, used if no ini file / not found in ini file
static char     GpTraceFileNameDefault[] = "~/BasicTrace.txt";
static char*    GpTraceFileName = GpTraceFileNameDefault;

// GbTraceOn:
// true = tracing is active, false = tracing is disabled, default = true
// Set to false initially if you want to activate tracing on demand with
// TraceCommand( "TraceOn" ), see below
static bool GbTraceOn = true;

// GbIncludePCodes:
// true = PCodes are written to trace, default = false, correspondents
// with TraceCommand( "PCodeOn" / "PCodeOff" ), see below
static bool GbIncludePCodes = false;

// GbInitOnlyAtOfficeStart:
// true = Tracing is only intialized onces after Office start when
// Basic runs the first time. Further calls to Basic, e.g. via events
// use the same output file. The trace ini file is not read again.
static bool GbInitOnlyAtOfficeStart = false;

static int  GnIndentPerCallLevel = 4;
static int  GnIndentForPCode = 2;

/*
    With trace enabled the runtime function TraceCommand
    can be used to influence the trace functionality
    from within the running Basic macro.

    Format: TraceCommand( command as String [, param as Variant] )

    Supported commands (command is NOT case sensitive):
    TraceCommand "TraceOn"          sets GbTraceOn = true
    TraceCommand "TraceOff"         sets GbTraceOn = false

    TraceCommand "PCodeOn"          sets GbIncludePCodes = true
    TraceCommand "PCodeOff"         sets GbIncludePCodes = false

    TraceCommand "Print", aVal      writes aVal into the trace file as
                                    long as it can be converted to string
*/

#ifdef DBG_TRACE_PROFILING

#include <algorithm>
#include <stack>
#include "canvas/elapsedtime.hxx"

//*** Profiling ***
// GbTimerOn:
// true = including time stamps
static bool GbTimerOn = true;

// GbTimeStampForEachStep:
// true = prints time stamp after each command / pcode (very slow)
static bool GbTimeStampForEachStep = false;

// GbBlockAllAfterFirstFunctionUsage:
// true = everything (commands, pcodes, functions) is only printed
// for the first usage (improves performance when tracing / pro-
// filing large macros)
static bool GbBlockAllAfterFirstFunctionUsage = false;

// GbBlockStepsAfterFirstFunctionUsage:
// true = commands / pcodes are only printed for the first time
// a function is executed. Afterwards only the entering/leaving
// messages are logged (improves performance when tracing / pro-
// filing large macros)
static bool GbBlockStepsAfterFirstFunctionUsage = false;

#endif


static void lcl_skipWhites( char*& rpc )
{
    while( *rpc == ' ' || *rpc == '\t' )
        ++rpc;
}

inline void lcl_findNextLine( char*& rpc, char* pe )
{
    // Find line end
    while( rpc < pe && *rpc != 13 && *rpc != 10 )
        ++rpc;

    // Read all
    while( rpc < pe && (*rpc == 13 || *rpc == 10) )
        ++rpc;
}

inline bool lcl_isAlpha( char c )
{
    bool bRet = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
    return bRet;
}

static void lcl_ReadIniFile( const char* pIniFileName )
{
    const int BUF_SIZE = 1000;
    static sal_Char TraceFileNameBuffer[BUF_SIZE];
    sal_Char Buffer[BUF_SIZE];
    sal_Char VarNameBuffer[BUF_SIZE];
    sal_Char ValBuffer[BUF_SIZE];

    FILE* pFile = fopen( pIniFileName ,"rb" );
    if( pFile == NULL )
        return;

    size_t nRead = fread( Buffer, 1, BUF_SIZE, pFile );

    // Scan
    char* pc = Buffer;
    char* pe = Buffer + nRead;
    while( pc < pe )
    {
        lcl_skipWhites( pc ); if( pc == pe ) break;

        // Read variable
        char* pVarStart = pc;
        while( pc < pe && lcl_isAlpha( *pc ) )
            ++pc;
        int nVarLen = pc - pVarStart;
        if( nVarLen == 0 )
        {
            lcl_findNextLine( pc, pe );
            continue;
        }
        strncpy( VarNameBuffer, pVarStart, nVarLen );
        VarNameBuffer[nVarLen] = '\0';

        // Check =
        lcl_skipWhites( pc ); if( pc == pe ) break;
        if( *pc != '=' )
            continue;
        ++pc;
        lcl_skipWhites( pc ); if( pc == pe ) break;

        // Read value
        char* pValStart = pc;
        while( pc < pe && *pc != 13 && *pc != 10 )
            ++pc;
        int nValLen = pc - pValStart;
        if( nValLen == 0 )
        {
            lcl_findNextLine( pc, pe );
            continue;
        }
        strncpy( ValBuffer, pValStart, nValLen );
        ValBuffer[nValLen] = '\0';

        // Match variables
        if( strcmp( VarNameBuffer, "GpTraceFileName") == 0 )
        {
            strcpy( TraceFileNameBuffer, ValBuffer );
            GpTraceFileName = TraceFileNameBuffer;
        }
        else
        if( strcmp( VarNameBuffer, "GbTraceOn") == 0 )
            GbTraceOn = (strcmp( ValBuffer, "true" ) == 0);
        else
        if( strcmp( VarNameBuffer, "GbIncludePCodes") == 0 )
            GbIncludePCodes = (strcmp( ValBuffer, "true" ) == 0);
        else
        if( strcmp( VarNameBuffer, "GbInitOnlyAtOfficeStart") == 0 )
            GbInitOnlyAtOfficeStart = (strcmp( ValBuffer, "true" ) == 0);
        else
        if( strcmp( VarNameBuffer, "GnIndentPerCallLevel") == 0 )
            GnIndentPerCallLevel = strtol( ValBuffer, NULL, 10 );
        else
        if( strcmp( VarNameBuffer, "GnIndentForPCode") == 0 )
            GnIndentForPCode = strtol( ValBuffer, NULL, 10 );
#ifdef DBG_TRACE_PROFILING
        else
        if( strcmp( VarNameBuffer, "GbTimerOn") == 0 )
            GbTimerOn = (strcmp( ValBuffer, "true" ) == 0);
        else
        if( strcmp( VarNameBuffer, "GbTimeStampForEachStep") == 0 )
            GbTimeStampForEachStep = (strcmp( ValBuffer, "true" ) == 0);
        else
        if( strcmp( VarNameBuffer, "GbBlockAllAfterFirstFunctionUsage") == 0 )
            GbBlockAllAfterFirstFunctionUsage = (strcmp( ValBuffer, "true" ) == 0);
        else
        if( strcmp( VarNameBuffer, "GbBlockStepsAfterFirstFunctionUsage") == 0 )
            GbBlockStepsAfterFirstFunctionUsage = (strcmp( ValBuffer, "true" ) == 0);
#endif
    }
    fclose( pFile );
}

struct TraceTextData
{
    rtl::OString m_aTraceStr_STMNT;
    rtl::OString m_aTraceStr_PCode;
};
typedef std::hash_map< sal_Int32, TraceTextData > PCToTextDataMap;
typedef std::hash_map< ::rtl::OUString, PCToTextDataMap*, ::rtl::OUStringHash, ::std::equal_to< ::rtl::OUString > > ModuleTraceMap;

ModuleTraceMap      GaModuleTraceMap;
ModuleTraceMap&     rModuleTraceMap = GaModuleTraceMap;

static FILE* GpGlobalFile = NULL;

static void lcl_lineOut( const char* pStr, const char* pPreStr = NULL, const char* pPostStr = NULL )
{
    if( GpGlobalFile != NULL )
    {
        fprintf( GpGlobalFile, "%s%s%s\n", pPreStr ? pPreStr : "", pStr, pPostStr ? pPostStr : "" );
        fflush( GpGlobalFile );
    }
}

const char* lcl_getSpaces( int nSpaceCount )
{
    static sal_Char Spaces[] = "                                                                                                    "
        "                                                                                                    "
        "                                                                                                    ";
    static int nAvailableSpaceCount = strlen( Spaces );
    static sal_Char* pSpacesEnd = Spaces + nAvailableSpaceCount;

    if( nSpaceCount > nAvailableSpaceCount )
        nSpaceCount = nAvailableSpaceCount;

    return pSpacesEnd - nSpaceCount;
}

static rtl::OString lcl_toOStringSkipLeadingWhites( const String& aStr )
{
    static sal_Char Buffer[1000];

    rtl::OString aOStr = OUStringToOString( rtl::OUString( aStr ), RTL_TEXTENCODING_ASCII_US );
    const sal_Char* pStr = aOStr.getStr();

    // Skip whitespace
    sal_Char c = *pStr;
    while( c == ' ' || c == '\t' )
    {
        pStr++;
        c = *pStr;
    }

    int nLen = strlen( pStr );
    strncpy( Buffer, pStr, nLen );
    Buffer[nLen] = 0;

    rtl::OString aORetStr( Buffer );
    return aORetStr;
}

String lcl_dumpMethodParameters( SbMethod* pMethod )
{
    String aStr;
    if( pMethod == NULL )
        return aStr;

    SbxError eOld = SbxBase::GetError();

    SbxArray* pParams = pMethod->GetParameters();
    SbxInfo* pInfo = pMethod->GetInfo();
    if ( pParams )
    {
        aStr += '(';
        // 0 is sub itself
        for ( sal_uInt16 nParam = 1; nParam < pParams->Count(); nParam++ )
        {
            SbxVariable* pVar = pParams->Get( nParam );
            DBG_ASSERT( pVar, "Parameter?!" );
            if ( pVar->GetName().Len() )
                aStr += pVar->GetName();
            else if ( pInfo )
            {
                const SbxParamInfo* pParam = pInfo->GetParam( nParam );
                if ( pParam )
                    aStr += pParam->aName;
            }
            aStr += '=';
            SbxDataType eType = pVar->GetType();
            if( eType & SbxARRAY )
                aStr += String( RTL_CONSTASCII_USTRINGPARAM( "..." ) );
            else if( eType != SbxOBJECT )
                aStr += pVar->GetString();
            if ( nParam < ( pParams->Count() - 1 ) )
                aStr += String( RTL_CONSTASCII_USTRINGPARAM( ", " ) );
        }
        aStr += ')';
    }

    SbxBase::ResetError();
    if( eOld != SbxERR_OK )
        SbxBase::SetError( eOld );

    return aStr;
}


// Public functions
static bool GbSavTraceOn = false;

#ifdef DBG_TRACE_PROFILING
static canvas::tools::ElapsedTime* GpTimer = NULL;
static double GdStartTime = 0.0;
static double GdLastTime = 0.0;
static bool GbBlockSteps = false;
static bool GbBlockAll = false;

struct FunctionItem
{
    String      m_aCompleteFunctionName;
    double      m_dTotalTime;
    double      m_dNetTime;
    int         m_nCallCount;
    bool        m_bBlockAll;
    bool        m_bBlockSteps;

    FunctionItem( void )
        : m_dTotalTime( 0.0 )
        , m_dNetTime( 0.0 )
        , m_nCallCount( 0 )
        , m_bBlockAll( false )
        , m_bBlockSteps( false )
    {}
};
typedef std::hash_map< ::rtl::OUString, FunctionItem*, ::rtl::OUStringHash, ::std::equal_to< ::rtl::OUString > > FunctionItemMap;

static std::stack< double >             GaCallEnterTimeStack;
static std::stack< FunctionItem* >      GaFunctionItemStack;
static FunctionItemMap                  GaFunctionItemMap;

bool compareFunctionNetTime( FunctionItem* p1, FunctionItem* p2 )
{
    return (p1->m_dNetTime > p2->m_dNetTime);
}

void lcl_printTimeOutput( void )
{
    // Overall time output
    lcl_lineOut( "" );
    lcl_lineOut( "***** Time Output *****" );
    char TimeBuffer[500];
    double dTotalTime = GpTimer->getElapsedTime() - GdStartTime;
    sprintf( TimeBuffer, "Total execution time = %f ms", dTotalTime*1000.0 );
    lcl_lineOut( TimeBuffer );
    lcl_lineOut( "" );

    if( GbTimerOn )
    {
        lcl_lineOut( "Functions:" );

        std::vector<FunctionItem*> avFunctionItems;

        FunctionItemMap::iterator it;
        for( it = GaFunctionItemMap.begin() ; it != GaFunctionItemMap.end() ; ++it )
        {
            FunctionItem* pFunctionItem = it->second;
            if( pFunctionItem != NULL )
                avFunctionItems.push_back( pFunctionItem );
        }

        std::sort( avFunctionItems.begin(), avFunctionItems.end(), compareFunctionNetTime );

        std::vector<FunctionItem*>::iterator itv;
        for( itv = avFunctionItems.begin() ; itv != avFunctionItems.end() ; ++itv )
        {
            FunctionItem* pFunctionItem = *itv;
            if( pFunctionItem != NULL )
            {
                rtl::OUString aCompleteFunctionName = pFunctionItem->m_aCompleteFunctionName;
                const char* pName = OUStringToOString( aCompleteFunctionName, RTL_TEXTENCODING_ASCII_US ).getStr();
                int nNameLen = aCompleteFunctionName.getLength();

                double dFctTotalTime = pFunctionItem->m_dTotalTime;
                double dFctNetTime = pFunctionItem->m_dNetTime;
                double dFctTotalTimePercent = 100.0 * dFctTotalTime / dTotalTime;
                double dFctNetTimePercent = 100.0 * dFctNetTime / dTotalTime;
                int nSpaceCount = 30 - nNameLen;
                if( nSpaceCount < 0 )
                    nSpaceCount = 2;
                sprintf( TimeBuffer, "%s:%sCalled %d times\t%f ms (%f%%) / net %f (%f%%) ms",
                    pName, lcl_getSpaces( nSpaceCount ), pFunctionItem->m_nCallCount,
                    dFctTotalTime*1000.0, dFctTotalTimePercent, dFctNetTime*1000.0, dFctNetTimePercent );
                lcl_lineOut( TimeBuffer );
            }
        }
    }
}
#endif


static bool GbInitTraceAlreadyCalled = false;

void dbg_InitTrace( void )
{
    if( GbInitOnlyAtOfficeStart && GbInitTraceAlreadyCalled )
    {
#ifdef DBG_TRACE_PROFILING
        if( GbTimerOn )
            GpTimer->continueTimer();
#endif
        GpGlobalFile = fopen( GpTraceFileName, "a+" );
        return;
    }
    GbInitTraceAlreadyCalled = true;

    if( const sal_Char* pcIniFileName = ::getenv( "OOO_BASICTRACEINI" ) )
        lcl_ReadIniFile( pcIniFileName );
    else if( GpTraceIniFile != NULL )
        lcl_ReadIniFile( GpTraceIniFile );

    GpGlobalFile = fopen( GpTraceFileName, "w" );
    GbSavTraceOn = GbTraceOn;
    if( !GbTraceOn )
        lcl_lineOut( "### Program started with trace off ###" );

#ifdef DBG_TRACE_PROFILING
    GpTimer = new canvas::tools::ElapsedTime();
    GdStartTime = GpTimer->getElapsedTime();
    GdLastTime = GdStartTime;
    GbBlockSteps = false;
    GbBlockAll = false;
#endif
}

void dbg_DeInitTrace( void )
{
    GbTraceOn = GbSavTraceOn;

#ifdef DBG_TRACE_PROFILING
    while( !GaCallEnterTimeStack.empty() )
        GaCallEnterTimeStack.pop();
    while( !GaFunctionItemStack.empty() )
        GaFunctionItemStack.pop();

    lcl_printTimeOutput();

    for( FunctionItemMap::iterator it = GaFunctionItemMap.begin() ; it != GaFunctionItemMap.end() ; ++it )
        delete it->second;
    GaFunctionItemMap.clear();

    if( GpGlobalFile )
    {
        fclose( GpGlobalFile );
        GpGlobalFile = NULL;
    }

    if( GbInitOnlyAtOfficeStart )
    {
        if( GbTimerOn )
            GpTimer->pauseTimer();
    }
    else
    {
        delete GpTimer;
    }
#endif
}

static sal_Int32 GnLastCallLvl = 0;

void dbg_tracePrint( const String& aStr, sal_Int32 nCallLvl, bool bCallLvlRelativeToCurrent )
{
    if( bCallLvlRelativeToCurrent )
        nCallLvl += GnLastCallLvl;

    int nIndent = nCallLvl * GnIndentPerCallLevel;
    lcl_lineOut( OUStringToOString( rtl::OUString( aStr ), RTL_TEXTENCODING_ASCII_US ).getStr(), lcl_getSpaces( nIndent ) );
}

void dbg_traceStep( SbModule* pModule, sal_uInt32 nPC, sal_Int32 nCallLvl )
{
    if( !GbTraceOn )
        return;

#ifdef DBG_TRACE_PROFILING
    if( GbBlockSteps || GbBlockAll )
        return;

    double dCurTime = 0.0;
    bool bPrintTimeStamp = false;
    if( GbTimerOn )
    {
        GpTimer->pauseTimer();
        dCurTime = GpTimer->getElapsedTime();
        bPrintTimeStamp = GbTimeStampForEachStep;
    }
#else
    bool bPrintTimeStamp = false;
#endif

    GnLastCallLvl = nCallLvl;

    SbModule* pTraceMod = pModule;
    if( pTraceMod->ISA(SbClassModuleObject) )
    {
        SbClassModuleObject* pClassModuleObj = (SbClassModuleObject*)(SbxBase*)pTraceMod;
        pTraceMod = pClassModuleObj->getClassModule();
    }

    String aModuleName = pTraceMod->GetName();
    ModuleTraceMap::iterator it = rModuleTraceMap.find( aModuleName );
    if( it == rModuleTraceMap.end() )
    {
        const char* pModuleNameStr = OUStringToOString( rtl::OUString( aModuleName ), RTL_TEXTENCODING_ASCII_US ).getStr();
        char Buffer[200];
        sprintf( Buffer, "TRACE ERROR: Unknown module \"%s\"", pModuleNameStr );
        lcl_lineOut( Buffer );
        return;
    }

    PCToTextDataMap* pInnerMap = it->second;
    if( pInnerMap == NULL )
    {
        lcl_lineOut( "TRACE INTERNAL ERROR: No inner map" );
        return;
    }

    PCToTextDataMap::iterator itInner = pInnerMap->find( nPC );
    if( itInner == pInnerMap->end() )
    {
        const char* pModuleNameStr = OUStringToOString( rtl::OUString( aModuleName ), RTL_TEXTENCODING_ASCII_US ).getStr();
        char Buffer[200];
        sprintf( Buffer, "TRACE ERROR: No info for PC = %d in module \"%s\"", (int)nPC, pModuleNameStr );
        lcl_lineOut( Buffer );
        return;
    }

    int nIndent = nCallLvl * GnIndentPerCallLevel;

    const TraceTextData& rTraceTextData = itInner->second;
    const rtl::OString& rStr_STMNT = rTraceTextData.m_aTraceStr_STMNT;
    bool bSTMT = false;
    if( rStr_STMNT.getLength() )
        bSTMT = true;

    char TimeBuffer[200];
#ifdef DBG_TRACE_PROFILING
    if( bPrintTimeStamp )
    {
        double dDiffTime = dCurTime - GdLastTime;
        GdLastTime = dCurTime;
        sprintf( TimeBuffer, "\t\t// Time = %f ms / += %f ms", dCurTime*1000.0, dDiffTime*1000.0 );
    }
#endif

    if( bSTMT )
    {
        lcl_lineOut( rStr_STMNT.getStr(), lcl_getSpaces( nIndent ),
            (bPrintTimeStamp && !GbIncludePCodes) ? TimeBuffer : NULL );
    }

    if( !GbIncludePCodes )
    {
#ifdef DBG_TRACE_PROFILING
        if( GbTimerOn )
            GpTimer->continueTimer();
#endif
        return;
    }

    nIndent += GnIndentForPCode;
    const rtl::OString& rStr_PCode = rTraceTextData.m_aTraceStr_PCode;
    if( rStr_PCode.getLength() )
    {
        lcl_lineOut( rStr_PCode.getStr(), lcl_getSpaces( nIndent ),
            bPrintTimeStamp ? TimeBuffer : NULL );
    }

#ifdef DBG_TRACE_PROFILING
    if( GbTimerOn )
        GpTimer->continueTimer();
#endif
}


void dbg_traceNotifyCall( SbModule* pModule, SbMethod* pMethod, sal_Int32 nCallLvl, bool bLeave )
{
    static const char* pSeparator = "' ================================================================================";

    if( !GbTraceOn )
        return;

#ifdef DBG_TRACE_PROFILING
    double dCurTime = 0.0;
    double dExecutionTime = 0.0;
    if( GbTimerOn )
    {
        dCurTime = GpTimer->getElapsedTime();
        GpTimer->pauseTimer();
    }
#endif

    GnLastCallLvl = nCallLvl;

    SbModule* pTraceMod = pModule;
    SbClassModuleObject* pClassModuleObj = NULL;
    if( pTraceMod->ISA(SbClassModuleObject) )
    {
        pClassModuleObj = (SbClassModuleObject*)(SbxBase*)pTraceMod;
        pTraceMod = pClassModuleObj->getClassModule();
    }

    String aCompleteFunctionName = pTraceMod->GetName();
    if( pMethod != NULL )
    {
        aCompleteFunctionName.AppendAscii( "::" );
        String aMethodName = pMethod->GetName();
        aCompleteFunctionName += aMethodName;
    }
    else
    {
        aCompleteFunctionName.AppendAscii( "/RunInit" );
    }

    bool bOwnBlockSteps = false;
#ifdef DBG_TRACE_PROFILING
    bool bOwnBlockAll = false;
    FunctionItem* pFunctionItem = NULL;
    if( GbTimerOn )
    {
        FunctionItemMap::iterator itFunctionItem = GaFunctionItemMap.find( aCompleteFunctionName );
        if( itFunctionItem != GaFunctionItemMap.end() )
            pFunctionItem = itFunctionItem->second;

        if( pFunctionItem == NULL )
        {
            DBG_ASSERT( !bLeave, "No FunctionItem in leave!" );

            pFunctionItem = new FunctionItem();
            pFunctionItem->m_aCompleteFunctionName = aCompleteFunctionName;
            GaFunctionItemMap[ aCompleteFunctionName ] = pFunctionItem;
        }
        else if( GbBlockAllAfterFirstFunctionUsage && !bLeave )
        {
            pFunctionItem->m_bBlockAll = true;
        }
        else if( GbBlockStepsAfterFirstFunctionUsage && !bLeave )
        {
            pFunctionItem->m_bBlockSteps = true;
        }

        if( bLeave )
        {
            bOwnBlockAll = GbBlockAll;
            bOwnBlockSteps = GbBlockSteps;
            GbBlockAll = false;
            GbBlockSteps = false;

            dExecutionTime = dCurTime - GaCallEnterTimeStack.top();
            GaCallEnterTimeStack.pop();

            pFunctionItem->m_dTotalTime += dExecutionTime;
            pFunctionItem->m_dNetTime += dExecutionTime;
            pFunctionItem->m_nCallCount++;

            GaFunctionItemStack.pop();
            if( !GaFunctionItemStack.empty() )
            {
                FunctionItem* pParentItem = GaFunctionItemStack.top();
                if( pParentItem != NULL )
                {
                    pParentItem->m_dNetTime -= dExecutionTime;

                    GbBlockSteps = pParentItem->m_bBlockSteps;
                    GbBlockAll = pParentItem->m_bBlockAll;
                }
            }
        }
        else
        {
            GbBlockSteps = bOwnBlockSteps = pFunctionItem->m_bBlockSteps;
            GbBlockAll = bOwnBlockAll = pFunctionItem->m_bBlockAll;

            GaCallEnterTimeStack.push( dCurTime );
            GaFunctionItemStack.push( pFunctionItem );
        }
    }

    if( bOwnBlockAll )
    {
        if( GbTimerOn )
            GpTimer->continueTimer();
        return;
    }
#endif

    if( nCallLvl > 0 )
        nCallLvl--;
    int nIndent = nCallLvl * GnIndentPerCallLevel;
    if( !bLeave && !bOwnBlockSteps )
    {
        lcl_lineOut( "" );
        lcl_lineOut( pSeparator, lcl_getSpaces( nIndent ) );
    }

    String aStr;
    if( bLeave )
    {
        if( !bOwnBlockSteps )
        {
            lcl_lineOut( "}", lcl_getSpaces( nIndent ) );
            aStr.AppendAscii( "' Leaving " );
        }
    }
    else
    {
        aStr.AppendAscii( "Entering " );
    }
    if( !bLeave || !bOwnBlockSteps )
        aStr += aCompleteFunctionName;

    if( !bOwnBlockSteps && pClassModuleObj != NULL )
    {
        aStr.AppendAscii( "[this=" );
        aStr += pClassModuleObj->GetName();
        aStr.AppendAscii( "]" );
    }
    if( !bLeave )
        aStr += lcl_dumpMethodParameters( pMethod );

    const char* pPostStr = NULL;
#ifdef DBG_TRACE_PROFILING
    char TimeBuffer[200];
    if( GbTimerOn && bLeave )
    {
        sprintf( TimeBuffer, "    // Execution Time = %f ms", dExecutionTime*1000.0 );
        pPostStr = TimeBuffer;
    }
#endif
    lcl_lineOut( (!bLeave || !bOwnBlockSteps) ? OUStringToOString( rtl::OUString( aStr ), RTL_TEXTENCODING_ASCII_US ).getStr() : "}",
        lcl_getSpaces( nIndent ), pPostStr );
    if( !bLeave )
        lcl_lineOut( "{", lcl_getSpaces( nIndent ) );

    if( bLeave && !bOwnBlockSteps )
        lcl_lineOut( "" );

#ifdef DBG_TRACE_PROFILING
    if( GbTimerOn )
        GpTimer->continueTimer();
#endif
}

void dbg_traceNotifyError( SbError nTraceErr, const String& aTraceErrMsg, bool bTraceErrHandled, sal_Int32 nCallLvl )
{
    if( !GbTraceOn )
        return;
#ifdef DBG_TRACE_PROFILING
    if( GbBlockSteps || GbBlockAll )
        return;
#endif
    GnLastCallLvl = nCallLvl;

    rtl::OString aOTraceErrMsg = OUStringToOString( rtl::OUString( aTraceErrMsg ), RTL_TEXTENCODING_ASCII_US );

    char Buffer[200];
    const char* pHandledStr = bTraceErrHandled ? " / HANDLED" : "";
    sprintf( Buffer, "*** ERROR%s, Id = %d, Msg = \"%s\" ***", pHandledStr, (int)nTraceErr, aOTraceErrMsg.getStr() );
    int nIndent = nCallLvl * GnIndentPerCallLevel;
    lcl_lineOut( Buffer, lcl_getSpaces( nIndent ) );
}

void dbg_RegisterTraceTextForPC( SbModule* pModule, sal_uInt32 nPC,
    const String& aTraceStr_STMNT, const String& aTraceStr_PCode )
{
    String aModuleName = pModule->GetName();
    ModuleTraceMap::iterator it = rModuleTraceMap.find( aModuleName );
    PCToTextDataMap* pInnerMap;
    if( it == rModuleTraceMap.end() )
    {
        pInnerMap = new PCToTextDataMap();
        rModuleTraceMap[ aModuleName ] = pInnerMap;
    }
    else
    {
        pInnerMap = it->second;
    }

    TraceTextData aData;

    rtl::OString aOTraceStr_STMNT = lcl_toOStringSkipLeadingWhites( aTraceStr_STMNT );
    aData.m_aTraceStr_STMNT = aOTraceStr_STMNT;

    rtl::OString aOTraceStr_PCode = lcl_toOStringSkipLeadingWhites( aTraceStr_PCode );
    aData.m_aTraceStr_PCode = aOTraceStr_PCode;

    (*pInnerMap)[nPC] = aData;
}

void RTL_Impl_TraceCommand( StarBASIC* pBasic, SbxArray& rPar, sal_Bool bWrite )
{
    (void)pBasic;
    (void)bWrite;

    if ( rPar.Count() < 2 )
    {
        StarBASIC::Error( SbERR_BAD_ARGUMENT );
        return;
    }

    String aCommand = rPar.Get(1)->GetString();

    if( aCommand.EqualsIgnoreCaseAscii( "TraceOn" ) )
        GbTraceOn = true;
    else
    if( aCommand.EqualsIgnoreCaseAscii( "TraceOff" ) )
        GbTraceOn = false;
    else
    if( aCommand.EqualsIgnoreCaseAscii( "PCodeOn" ) )
        GbIncludePCodes = true;
    else
    if( aCommand.EqualsIgnoreCaseAscii( "PCodeOff" ) )
        GbIncludePCodes = false;
    else
    if( aCommand.EqualsIgnoreCaseAscii( "Print" ) )
    {
        if ( rPar.Count() < 3 )
        {
            StarBASIC::Error( SbERR_BAD_ARGUMENT );
            return;
        }

        SbxError eOld = SbxBase::GetError();
        if( eOld != SbxERR_OK )
            SbxBase::ResetError();

        String aValStr = rPar.Get(2)->GetString();
        SbxError eErr = SbxBase::GetError();
        if( eErr != SbxERR_OK )
        {
            aValStr = String( RTL_CONSTASCII_USTRINGPARAM( "<ERROR converting value to String>" ) );
            SbxBase::ResetError();
        }

        char Buffer[500];
        const char* pValStr = OUStringToOString( rtl::OUString( aValStr ), RTL_TEXTENCODING_ASCII_US ).getStr();

        sprintf( Buffer, "### TRACE_PRINT: %s ###", pValStr );
        int nIndent = GnLastCallLvl * GnIndentPerCallLevel;
        lcl_lineOut( Buffer, lcl_getSpaces( nIndent ) );

        if( eOld != SbxERR_OK )
            SbxBase::SetError( eOld );
    }
}

#endif

// This routine is defined here, so that the
// compiler can be loaded as a discrete segment.

sal_Bool SbModule::Compile()
{
    if( pImage )
        return sal_True;
    StarBASIC* pBasic = PTR_CAST(StarBASIC,GetParent());
    if( !pBasic )
        return sal_False;
    SbxBase::ResetError();

    SbModule* pOld = GetSbData()->pCompMod;
    GetSbData()->pCompMod = this;

    SbiParser* pParser = new SbiParser( (StarBASIC*) GetParent(), this );
    while( pParser->Parse() ) {}
    if( !pParser->GetErrors() )
        pParser->aGen.Save();
    delete pParser;
    // for the disassembler
    if( pImage )
        pImage->aOUSource = aOUSource;

    GetSbData()->pCompMod = pOld;

    // compiling a module, the module-global
    // variables of all modules become invalid
    sal_Bool bRet = IsCompiled();
    if( bRet )
    {
        if( !this->ISA(SbObjModule) )
            pBasic->ClearAllModuleVars();
        RemoveVars(); // remove 'this' Modules variables
        // clear all method statics
        for( sal_uInt16 i = 0; i < pMethods->Count(); i++ )
        {
            SbMethod* p = PTR_CAST(SbMethod,pMethods->Get( i ) );
            if( p )
                p->ClearStatics();
        }

        // #i31510 Init other libs only if Basic isn't running
        if( GetSbData()->pInst == NULL )
        {
            SbxObject* pParent_ = pBasic->GetParent();
            if( pParent_ )
                pBasic = PTR_CAST(StarBASIC,pParent_);
            if( pBasic )
                pBasic->ClearAllModuleVars();
        }
    }

    return bRet;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */