1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <stardiv/uno/repos/implementationregistration.hxx>
23 #include <stardiv/uno/script/script.hxx>
24 #include <stardiv/uno/beans/exactname.hxx>
26 #include <rtl/ustring.hxx>
27 #include <osl/diagnose.h>
28 #include <usr/services.hxx>
29 #include <vcl/svapp.hxx>
30 #include <usr/ustring.hxx>
31 #include <usr/weak.hxx>
32 #include <tools/string.hxx>
33 #include <osl/conditn.hxx>
37 using ::rtl::StringToOUString
;
40 #define PCHAR_TO_USTRING(x) StringToOUString(String(x),CHARSET_SYSTEM)
44 class NullEngineListenerRef
: public XEngineListenerRef
46 virtual void interrupt(const InterruptEngineEvent
& Evt
) THROWS( (UsrSystemException
) ) {}
47 virtual void running(const EventObject
& Evt
) THROWS( (UsrSystemException
) ) {}
48 virtual void finished(const FinishEngineEvent
& Evt
) THROWS( (UsrSystemException
) ) {}
51 #define USTRING_TO_PCHAR(x) OUStringToString(x , CHARSET_DONTKNOW ).GetCharStr()
54 public XEngineListener
,
63 m_bIsTerminating
= FALSE
;
68 CmdDebugger( XDebuggingRef
*p
, XEngineRef
*pEngine
, XInvokationRef
*pInvokation
)
70 attach( p
, pEngine
, pInvokation
);
75 if( m_pDebuggingRef
) {
80 BOOL
queryInterface( Uik aUik
, XInterfaceRef
& rOut
);
81 void acquire() { OWeakObject::acquire(); }
82 void release() { OWeakObject::release(); }
83 void* getImplementation(Reflection
*p
) { return OWeakObject::getImplementation(p
); }
86 void attach( XDebuggingRef
*p
, XEngineRef
*pEngine
, XInvokationRef
*pInvokation
)
89 m_pEngineRef
= pEngine
;
90 m_pInvokationRef
= pInvokation
;
92 m_bIsTerminating
= FALSE
;
98 virtual void disposing( const EventObject
&o
)
100 if( m_pDebuggingRef
) {
104 virtual void interrupt(const InterruptEngineEvent
& Evt
) THROWS( (UsrSystemException
) )
106 if( m_pDebuggingRef
&& ! m_bIsTerminating
) {
107 (*m_pDebuggingRef
)->stop();
108 fprintf( stderr
, "%s\n" , USTRING_TO_PCHAR(Evt
.ErrorMessage
) );
109 fprintf( stderr
, "%s.%s (%d)\n", USTRING_TO_PCHAR(Evt
.SourceCode
),
110 USTRING_TO_PCHAR(Evt
.Name
),
112 m_aDebugCondition
.set();
117 virtual void running(const EventObject
& Evt
) THROWS( (UsrSystemException
) )
119 if( m_pDebuggingRef
&& ! m_bIsTerminating
) {
120 (*m_pDebuggingRef
)->stop();
122 m_aDebugCondition
.set();
124 fprintf( stderr
, "%s\n" , "Script starts\n" );
128 virtual void finished(const FinishEngineEvent
& Evt
) THROWS( (UsrSystemException
) )
130 if( m_pDebuggingRef
&& ! m_bIsTerminating
) {
131 m_aDebugCondition
.set();
132 m_bIsRunning
= FALSE
;
133 fprintf( stderr
, "%s\n", USTRING_TO_PCHAR( Evt
.ErrorMessage
) );
137 void dumpIntrospectionToStream( const XIntrospectionAccessRef
&, FILE *f
);
138 void dumpVarToStream( const char *pcName
, const UsrAny
&any
, FILE *f
);
144 osl::Condition m_aDebugCondition
;
145 XDebuggingRef
*m_pDebuggingRef
;
146 XEngineRef
*m_pEngineRef
;
147 XInvokationRef
*m_pInvokationRef
;
149 int m_bIsTerminating
; // The listeners ignore everything when set
154 void CmdDebugger::cmdLine()
157 fprintf( stderr
, "entering debugger\n" );
160 m_aDebugCondition
.wait();
162 fprintf( stderr
, "(debug %d) : " , m_bIsRunning
);
164 fgets( pcLine
, 79 , stdin
);
166 if( strlen( pcLine
) ) pcLine
[strlen(pcLine
)-1] =0;
167 String
sLine( pcLine
);
169 if( ! strcmp( pcLine
, "g" ) ) {
171 m_aDebugCondition
.reset();
172 (*m_pDebuggingRef
)->doContinue();
174 else fprintf( stderr
,"no script running !\n" );
176 else if( ! strcmp( pcLine
, "s" ) ) {
178 m_aDebugCondition
.reset();
179 (*m_pDebuggingRef
)->stepOver();
181 else fprintf(stderr
, "no script running !\n" );
183 else if( ! strcmp( pcLine
, "so" ) ) {
185 m_aDebugCondition
.reset();
186 (*m_pDebuggingRef
)->stepOut();
188 else fprintf(stderr
, "no script running !\n" );
190 else if( ! strcmp( pcLine
, "si" ) ) {
192 m_aDebugCondition
.reset();
193 (*m_pDebuggingRef
)->stepIn();
195 else fprintf(stderr
, "no script running !\n" );
197 else if( ! strncmp( pcLine
, "sbp" , 3 ) ){
199 (*m_pDebuggingRef
)->setBreakPoint( UString( L
"<string>" ),
200 atoi(&pcLine
[3]) , TRUE
);
203 else if( ! strncmp( pcLine
, "rbp" , 3 ) ){
205 (*m_pDebuggingRef
)->setBreakPoint( UString( L
"<string>" ),
206 atoi(&pcLine
[3]) , FALSE
);
209 else if( ! strncmp( pcLine
, "dv " , 3 ) ) {
212 if( sLine
.GetQuotedTokenCount( String("''"),' ' ) == 3 ) {
213 nCallstack
= atoi( sLine
.GetQuotedToken( 3 , String("''"), ' ' ).GetCharStr() );
216 UString str
= (*m_pDebuggingRef
)->dumpVariable(
217 PCHAR_TO_USTRING( &pcLine
[3]),nCallstack
);
218 fprintf( stderr
, "%s\n" , USTRING_TO_PCHAR( str
) );
221 else if( ! strncmp( pcLine
, "sv " , 3 ) ) {
223 if( sLine
.GetQuotedTokenCount( String("''"),' ' ) == 3 ) {
224 nCallstack
= atoi( sLine
.GetQuotedToken( 3 , String("''"), ' ' ).GetCharStr() );
226 (*m_pDebuggingRef
)->setVariable(
227 StringToOUString( sLine
.GetQuotedToken( 1 , String("''"), ' ' ), CHARSET_SYSTEM
),
228 StringToOUString( sLine
.GetQuotedToken( 2 , String("''"), ' ' ), CHARSET_SYSTEM
),
232 else if( ! strncmp( pcLine
, "ci" ,2 ) ) {
235 ContextInformation ci
= (*m_pDebuggingRef
)->getContextInformation(atoi(&pcLine
[2]));
238 fprintf( stderr
, "File %s (%d)\n", USTRING_TO_PCHAR(ci
.Name
),
240 fprintf( stderr
, "Available variables : \n" );
241 aUString
= ci
.LocalVariableNames
.getArray();
242 iMax
= ci
.LocalVariableNames
.getLen();
244 for( i
= 0 ; i
< iMax
; i
++ ) {
245 fprintf( stderr
, " %s\n" , USTRING_TO_PCHAR( aUString
[i
]) );
249 else if ( !strcmp( pcLine
, "d" ) ) {
252 Sequence
<UString
> seq
= (*m_pDebuggingRef
)->getStackTrace();
254 aUString
= seq
.getArray();
255 int iMax
= seq
.getLen();
256 for( int i
= 0; i
< iMax
; i
++ ) {
257 fprintf( stderr
, "%s\n" , USTRING_TO_PCHAR( aUString
[i
] ) );
261 else if( !strcmp( pcLine
, "c" ) ) {
263 (*m_pEngineRef
)->cancel();
264 m_aDebugCondition
.reset();
266 else fprintf( stderr
,"no script running !\n" );
268 else if( !strcmp( pcLine
, "q" ) ) {
270 m_aDebugCondition
.reset();
271 (*m_pEngineRef
)->cancel();
274 m_bIsTerminating
= TRUE
;
275 fprintf(stderr
, "Debugger terminates\n" );
279 else if( ! strcmp( pcLine
, "id" ) ) {
281 XIntrospectionAccessRef ref
= (*m_pInvokationRef
)->getIntrospection();
283 dumpIntrospectionToStream( ref
, stderr
);
287 else if( ! strncmp( pcLine
, "idv" , 3) ) {
289 UsrAny any
= (*m_pInvokationRef
)->getValue( PCHAR_TO_USTRING( &(pcLine
[4]) ) );
290 dumpVarToStream( &(pcLine
[4]) , any
, stderr
);
292 catch(UnknownPropertyException
& e
) {
293 fprintf( stderr
, "UnknownPropertyException\n" );
295 catch(IllegalArgumentException
& e
) {
296 fprintf( stderr
, "IllegalArgumentException\n" );
299 else if( !strcmp( pcLine
, "t" ) ) {
301 else if( !strcmp( pcLine
, "h" ) ) {
302 fprintf( stderr
, "\nvalid commands :\n"
307 "Set BreakPoint : sbp Line [ModuleName]\n"
308 "Remove BreakPoint : rbp [Line] [ModuleName]\n"
309 "via XDebugging Interface :\n"
310 " dump Variable : dv varname [CallStack]\n"
311 " set Variable : sv varname value [CallStack]\n"
312 "globals via XInvokation Interface :\n"
313 " dump Global vars : id\n"
314 " dump Variable : idv varname\n"
315 " set Variable : isv varname value\n"
316 "ContextInformation : ci\n"
317 "Dump callstack : d\n"
318 "Cancel : c (stops actual script)\n"
319 "Quit : q (exits debugger)\n"
322 else if( ! strlen( pcLine
) ) {
325 fprintf( stderr
, "unknown command %s\n" , pcLine
);
330 void CmdDebugger::dumpIntrospectionToStream( const XIntrospectionAccessRef
&ref
, FILE *f
)
333 fprintf( stderr
, "Callable Attributes (Methods) :\n" );
334 Sequence
<XIdlMethodRef
> seq
= ref
->getMethods( 0 );
336 XIdlMethodRef
*aRef
= seq
.getArray();
337 for( i
= 0; i
< iMax
; i
++ ) {
338 fprintf( f
, " %s\n" , USTRING_TO_PCHAR( aRef
[i
]->getName( ) ) );
341 fprintf( stderr
, "Other attributes\n" );
342 Sequence
<Property
> seqProp
= ref
->getProperties( 0 );
343 iMax
= seqProp
.getLen();
344 Property
*aProp
= seqProp
.getArray();
345 for( i
= 0; i
< iMax
; i
++ ) {
346 fprintf( f
, " %s %s\n" , USTRING_TO_PCHAR( aProp
[i
].Type
->getName() ),
347 USTRING_TO_PCHAR( aProp
[i
].Name
) );
352 void CmdDebugger::dumpVarToStream( const char *pc
, const UsrAny
&aValue
, FILE *f
)
354 TypeClass type
= aValue
.getReflection()->getTypeClass();
356 if( TypeClass_INT
== type
) {
357 fprintf( f
, "INT32 %s : %d\n" , pc
, aValue
.getINT32() );
359 else if( TypeClass_ENUM
== type
) {
360 fprintf( f
, "ENUM %s : %d\n", pc
, aValue
.getEnumAsINT32() );
362 else if( TypeClass_STRING
== type
) {
363 fprintf( f
, "STRING %s : %s\n" , pc
, USTRING_TO_PCHAR( aValue
.getString()) );
365 else if( TypeClass_BOOLEAN
== type
) {
366 fprintf( f
, "BOOL %s : %d\n", pc
, aValue
.getBOOL() );
368 else if( TypeClass_CHAR
== type
) {
369 fprintf( f
, "char %s : %d\n", pc
, ( INT32
) aValue
.getChar() );
371 else if( TypeClass_SHORT
== type
) {
372 fprintf( f
, "INT16 %s : %d\n", pc
, (INT32
) aValue
.getINT16());
374 else if( TypeClass_LONG
== type
) {
375 fprintf( f
, "LONG %s : %d\n", pc
, (INT32
) aValue
.getINT32());
377 else if( TypeClass_UNSIGNED_SHORT
== type
) {
378 fprintf( f
, "UINT16 %s : %d\n", pc
, (INT32
) aValue
.getUINT16() );
380 else if( TypeClass_UNSIGNED_BYTE
== type
) {
381 fprintf( f
, "Byte %s : %d\n", pc
, (INT32
) aValue
.getBYTE() );
383 else if( TypeClass_UNSIGNED_INT
== type
) {
384 fprintf( f
, "UINT32 %s : %d\n", pc
, aValue
.getUINT32() );
386 else if( TypeClass_FLOAT
== type
) {
387 fprintf( f
, "float %s : %f\n" , pc
, aValue
.getFloat() );
389 else if( TypeClass_DOUBLE
== type
) {
390 fprintf( f
, "double %s : %f\n" , pc
, aValue
.getDouble() );
392 else if( TypeClass_VOID
== type
) {
393 fprintf( f
, "void %s :\n" , pc
);
395 else if( TypeClass_INTERFACE
== type
) {
396 // Check, what has been put in
397 if( aValue
.getReflection() == XPropertySet_getReflection() ) {
399 XPropertySetRef
*pRef
= ( XPropertySetRef
* ) aValue
.get();
400 XPropertySetInfoRef refInfo
= (*pRef
)->getPropertySetInfo();
401 Sequence
< Property
> seq
= refInfo
->getProperties();
402 int i
,iMax
= seq
.getLen();
405 pArray
= seq
.getArray();
406 fprintf( stderr
, "Property List :\n" );
407 for( i
= 0; i
< iMax
; i
++ ) {
408 fprintf( f
, "%s\t %s\n" , USTRING_TO_PCHAR(pArray
[i
].Type
->getName()),
409 USTRING_TO_PCHAR( pArray
[i
].Name
) );
412 else if( aValue
.getReflection() == XInvokation_getReflection() ) {
413 XInvokationRef
*pRef
= ( XInvokationRef
* ) aValue
.get();
414 XIntrospectionAccessRef refIntro
= (*pRef
)->getIntrospection();
416 dumpIntrospectionToStream( refIntro
, stderr
);
419 else if( TypeClass_SEQUENCE
== type
) {
420 fprintf( f
, "%s Sequence \n" , pc
);
423 SequenceReflection
*pSeqRefl
= ( SequenceReflection
* ) aValue
.getReflection();
425 int i
,iMax
= pSeqRefl
->getLen( aValue
);
427 for( i
= 0 ; i
< iMax
; i
++ ) {
428 dumpVarToStream( s
.GetCharStr() , pSeqRefl
->get( aValue
, i
) , stderr
);
432 fprintf( f
, "%s : unknown %d\n" , pc
, type
);
437 void CmdDebugger::detach()
439 OSL_ASSERT( m_pDebuggingRef
);
441 m_bIsRunning
= FALSE
;
444 m_pInvokationRef
= 0;
447 // Methoden von XInterface
448 BOOL
CmdDebugger::queryInterface( Uik aUik
, XInterfaceRef
& rOut
)
450 if( aUik
== XEngineListener::getSmartUik() )
451 rOut
= (XEngineListener
*)this;
453 return OWeakObject::queryInterface( aUik
, rOut
);
465 int SAL_CALL
main (int argc
, char **argv
)
467 XMultiServiceFactoryRef xSMgr
= createRegistryServiceManager();
468 registerUsrServices( xSMgr
);
469 setProcessServiceManager( xSMgr
);
471 XInterfaceRef x
= xSMgr
->createInstance( L
"stardiv.uno.repos.ImplementationRegistration" );
472 XImplementationRegistrationRef
xReg( x
, USR_QUERY
);
473 sal_Char szBuf
[1024];
475 ORealDynamicLoader::computeModuleName( "pythonengine", szBuf
, 1024 );
476 UString
aDllName( StringToOUString( szBuf
, CHARSET_SYSTEM
) );
477 xReg
->registerImplementation( L
"stardiv.loader.SharedLibrary", aDllName
, XSimpleRegistryRef() );
479 ORealDynamicLoader::computeModuleName( "aps", szBuf
, 1024 );
480 aDllName
= UString( StringToOUString( szBuf
, CHARSET_SYSTEM
) );
481 xReg
->registerImplementation( L
"stardiv.loader.SharedLibrary", aDllName
, XSimpleRegistryRef() );
483 XInterfaceRef y
= xSMgr
->createInstance( L
"stardiv.script.Python" );
484 XEngineRef
yEngine( y
, USR_QUERY
);
486 x
= xSMgr
->createInstance( L
"stardiv.script.Python" );
487 XEngineRef
xEngine( x
, USR_QUERY
);
492 Sequence
<UsrAny
> args(3);
493 UsrAny
*pArray
= args
.getArray();
494 pArray
[0].setString( L
"Arg_0" );
495 pArray
[1].setString( L
"Arg_1" );
496 pArray
[2].setString( L
"Arg_2" );
499 Script
= StringToOUString( String( argv
[2] ) , CHARSET_DONTKNOW
);
502 XInvokationRef
xInvokation( x
, USR_QUERY
);
503 XDebuggingRef
xDebug( x
, USR_QUERY
);
505 CmdDebugger
*pDbg
= new CmdDebugger( &xDebug
, &xEngine
, &xInvokation
);
507 XEngineListenerRef
xDebugRef( (XEngineListener
*) pDbg
, USR_QUERY
);
508 xEngine
->addEngineListener( xDebugRef
);
511 if( argc
>1 && ! strcmp( argv
[1] , "1" ) ) {
512 fprintf( stderr
, "one thread only\n" );
513 Script
= UString( L
"print 'Hello World'\n" );
514 xEngine
->runAsync( Script
, XInterfaceRef(), args
, XEngineListenerRef() );
516 else if( argc
>1 && ! strcmp( argv
[1] , "2" ) ) {
518 xEngine
->runAsync( UString( L
"x=1\nprint 1\n") , XInterfaceRef(), args
, XEngineListenerRef() );
519 xEngine
->runAsync( UString( L
"x=x+1\nprint 2\n") , XInterfaceRef(), args
, XEngineListenerRef() );
520 xEngine
->runAsync( UString( L
"x=x+1\nprint 3\n") , XInterfaceRef(), args
, XEngineListenerRef());
521 xEngine
->runAsync( UString( L
"x=x+1\nprint 4\n") , XInterfaceRef(), args
, XEngineListenerRef() );
526 else if( argc
>1 && ! strcmp( argv
[1] , "3" ) ) {
528 fprintf( stderr
, "1st thread in engine y, next 5 threads in engine x\n" );
529 yEngine
->runAsync( UString( L
"print 1\n") , XInterfaceRef(), args
, XEngineListenerRef() );
530 xEngine
->runAsync( UString( L
"print 2\n") , XInterfaceRef(), args
, XEngineListenerRef() );
531 xEngine
->runAsync( UString( L
"print 3\n") , XInterfaceRef(), args
, XEngineListenerRef() );
532 xEngine
->runAsync( UString( L
"print 4\n") , XInterfaceRef(), args
, XEngineListenerRef());
533 xEngine
->runAsync( UString( L
"print 5\n") , XInterfaceRef(), args
, XEngineListenerRef());
534 xEngine
->runAsync( UString( L
"print 6\n") , XInterfaceRef(), args
, XEngineListenerRef());
540 xEngine
->removeEngineListener( xDebugRef
);
542 xReg
->revokeImplementation( aDllName
, XSimpleRegistryRef() );
544 fprintf( stderr
, "main terminates\n" );
548 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */