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
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_extensions.hxx"
26
27 #include <stdio.h>
28 #include <stardiv/uno/repos/implementationregistration.hxx>
29 #include <stardiv/uno/script/script.hxx>
30 #include <stardiv/uno/beans/exactname.hxx>
31
32 #include <rtl/ustring.hxx>
33 #include <vos/dynload.hxx>
34 #include <vos/diagnose.hxx>
35 #include <usr/services.hxx>
36 #include <vcl/svapp.hxx>
37 #include <usr/ustring.hxx>
38 #include <usr/weak.hxx>
39 #include <tools/string.hxx>
40 #include <vos/conditn.hxx>
41
42 using namespace rtl;
43 using namespace vos;
44 using namespace usr;
45
46 #define PCHAR_TO_USTRING(x) StringToOUString(String(x),CHARSET_SYSTEM)
47
48
49
50 class NullEngineListenerRef : public XEngineListenerRef
51 {
interrupt(const InterruptEngineEvent & Evt)52 virtual void interrupt(const InterruptEngineEvent& Evt) THROWS( (UsrSystemException) ) {}
running(const EventObject & Evt)53 virtual void running(const EventObject& Evt) THROWS( (UsrSystemException) ) {}
finished(const FinishEngineEvent & Evt)54 virtual void finished(const FinishEngineEvent& Evt) THROWS( (UsrSystemException) ) {}
55 };
56
57 #define USTRING_TO_PCHAR(x) OUStringToString(x , CHARSET_DONTKNOW ).GetCharStr()
58
59 class CmdDebugger :
60 public XEngineListener,
61 public OWeakObject
62 {
63 public:
64
CmdDebugger()65 CmdDebugger()
66 {
67 m_pDebuggingRef = 0;
68 m_pEngineRef = 0;
69 m_bIsTerminating = FALSE;
70 m_bIsRunning = FALSE;
71 }
72
73
CmdDebugger(XDebuggingRef * p,XEngineRef * pEngine,XInvokationRef * pInvokation)74 CmdDebugger( XDebuggingRef *p, XEngineRef *pEngine , XInvokationRef *pInvokation)
75 {
76 attach( p , pEngine , pInvokation );
77 }
78
~CmdDebugger()79 ~CmdDebugger()
80 {
81 if( m_pDebuggingRef ) {
82 detach();
83 }
84 }
85
86 BOOL queryInterface( Uik aUik, XInterfaceRef & rOut );
acquire()87 void acquire() { OWeakObject::acquire(); }
release()88 void release() { OWeakObject::release(); }
getImplementation(Reflection * p)89 void* getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
90
91
attach(XDebuggingRef * p,XEngineRef * pEngine,XInvokationRef * pInvokation)92 void attach( XDebuggingRef *p , XEngineRef *pEngine , XInvokationRef *pInvokation )
93 {
94 m_pDebuggingRef = p;
95 m_pEngineRef = pEngine;
96 m_pInvokationRef = pInvokation;
97 m_bIsRunning = FALSE;
98 m_bIsTerminating = FALSE;
99 }
100
101 void detach( );
102
103
disposing(const EventObject & o)104 virtual void disposing( const EventObject &o )
105 {
106 if( m_pDebuggingRef ) {
107 detach();
108 }
109 }
interrupt(const InterruptEngineEvent & Evt)110 virtual void interrupt(const InterruptEngineEvent& Evt) THROWS( (UsrSystemException) )
111 {
112 if( m_pDebuggingRef && ! m_bIsTerminating ) {
113 (*m_pDebuggingRef)->stop();
114 fprintf( stderr, "%s\n" , USTRING_TO_PCHAR(Evt.ErrorMessage ) );
115 fprintf( stderr, "%s.%s (%d)\n", USTRING_TO_PCHAR(Evt.SourceCode),
116 USTRING_TO_PCHAR(Evt.Name ),
117 Evt.StartLine );
118 m_aDebugCondition.set();
119 m_bIsRunning = TRUE;
120 }
121 }
122
running(const EventObject & Evt)123 virtual void running(const EventObject& Evt) THROWS( (UsrSystemException) )
124 {
125 if( m_pDebuggingRef && ! m_bIsTerminating ) {
126 (*m_pDebuggingRef)->stop();
127
128 m_aDebugCondition.set();
129 m_bIsRunning = TRUE;
130 fprintf( stderr, "%s\n" , "Script starts\n" );
131 }
132 }
133
finished(const FinishEngineEvent & Evt)134 virtual void finished(const FinishEngineEvent& Evt) THROWS( (UsrSystemException) )
135 {
136 if( m_pDebuggingRef && ! m_bIsTerminating ) {
137 m_aDebugCondition.set();
138 m_bIsRunning = FALSE;
139 fprintf( stderr , "%s\n", USTRING_TO_PCHAR( Evt.ErrorMessage ) );
140 }
141 }
142
143 void dumpIntrospectionToStream( const XIntrospectionAccessRef &, FILE *f );
144 void dumpVarToStream( const char *pcName, const UsrAny &any, FILE *f );
145
146
147 void cmdLine();
148 protected:
149
150 OCondition m_aDebugCondition;
151 XDebuggingRef *m_pDebuggingRef;
152 XEngineRef *m_pEngineRef;
153 XInvokationRef *m_pInvokationRef;
154 int m_bIsRunning;
155 int m_bIsTerminating; // The listeners ignore everything when set
156 };
157
158
159
cmdLine()160 void CmdDebugger::cmdLine()
161 {
162 char pcLine[80];
163 fprintf( stderr, "entering debugger\n" );
164 while( TRUE ) {
165
166 m_aDebugCondition.wait();
167
168 fprintf( stderr , "(debug %d) : " , m_bIsRunning );
169 fflush( stderr);
170 fgets( pcLine , 79 , stdin );
171
172 if( strlen( pcLine) ) pcLine[strlen(pcLine)-1] =0;
173 String sLine( pcLine );
174
175 if( ! strcmp( pcLine , "g" ) ) {
176 if( m_bIsRunning ) {
177 m_aDebugCondition.reset();
178 (*m_pDebuggingRef)->doContinue();
179 }
180 else fprintf( stderr,"no script running !\n" );
181 }
182 else if( ! strcmp( pcLine , "s" ) ) {
183 if( m_bIsRunning ) {
184 m_aDebugCondition.reset();
185 (*m_pDebuggingRef)->stepOver();
186 }
187 else fprintf(stderr, "no script running !\n" );
188 }
189 else if( ! strcmp( pcLine , "so" ) ) {
190 if( m_bIsRunning ) {
191 m_aDebugCondition.reset();
192 (*m_pDebuggingRef)->stepOut();
193 }
194 else fprintf(stderr, "no script running !\n" );
195 }
196 else if( ! strcmp( pcLine , "si" ) ) {
197 if( m_bIsRunning ) {
198 m_aDebugCondition.reset();
199 (*m_pDebuggingRef)->stepIn();
200 }
201 else fprintf(stderr, "no script running !\n" );
202 }
203 else if( ! strncmp( pcLine , "sbp" , 3 ) ){
204 if( m_bIsRunning ) {
205 (*m_pDebuggingRef)->setBreakPoint( UString( L"<string>" ),
206 atoi(&pcLine[3]) , TRUE );
207 }
208 }
209 else if( ! strncmp( pcLine , "rbp" , 3 ) ){
210 if( m_bIsRunning ) {
211 (*m_pDebuggingRef)->setBreakPoint( UString( L"<string>" ),
212 atoi(&pcLine[3]) , FALSE );
213 }
214 }
215 else if( ! strncmp( pcLine , "dv " , 3 ) ) {
216 if( m_bIsRunning ) {
217 int nCallstack = 0;
218 if( sLine.GetQuotedTokenCount( String("''"),' ' ) == 3 ) {
219 nCallstack = atoi( sLine.GetQuotedToken( 3 , String("''"), ' ' ).GetCharStr() );
220 }
221
222 UString str = (*m_pDebuggingRef)->dumpVariable(
223 PCHAR_TO_USTRING( &pcLine[3]),nCallstack);
224 fprintf( stderr, "%s\n" , USTRING_TO_PCHAR( str ) );
225 }
226 }
227 else if( ! strncmp( pcLine , "sv " , 3 ) ) {
228 int nCallstack = 0;
229 if( sLine.GetQuotedTokenCount( String("''"),' ' ) == 3 ) {
230 nCallstack = atoi( sLine.GetQuotedToken( 3 , String("''"), ' ' ).GetCharStr() );
231 }
232 (*m_pDebuggingRef)->setVariable(
233 StringToOUString( sLine.GetQuotedToken( 1 , String("''"), ' ' ), CHARSET_SYSTEM ),
234 StringToOUString( sLine.GetQuotedToken( 2 , String("''"), ' ' ), CHARSET_SYSTEM ),
235 nCallstack );
236
237 }
238 else if( ! strncmp( pcLine , "ci" ,2 ) ) {
239 if( m_bIsRunning ) {
240 UString *aUString ;
241 ContextInformation ci = (*m_pDebuggingRef)->getContextInformation(atoi(&pcLine[2]));
242 int i,iMax;
243
244 fprintf( stderr, "File %s (%d)\n", USTRING_TO_PCHAR(ci.Name),
245 ci.StartLine );
246 fprintf( stderr, "Available variables : \n" );
247 aUString = ci.LocalVariableNames.getArray();
248 iMax = ci.LocalVariableNames.getLen();
249
250 for( i = 0 ; i < iMax ; i++ ) {
251 fprintf( stderr, " %s\n" , USTRING_TO_PCHAR( aUString[i]) );
252 }
253 }
254 }
255 else if ( !strcmp( pcLine , "d" ) ) {
256 if( m_bIsRunning ) {
257 UString * aUString ;
258 Sequence<UString> seq = (*m_pDebuggingRef)->getStackTrace();
259
260 aUString = seq.getArray();
261 int iMax = seq.getLen();
262 for( int i = 0; i < iMax ; i++ ) {
263 fprintf( stderr , "%s\n" , USTRING_TO_PCHAR( aUString[i] ) );
264 }
265 }
266 }
267 else if( !strcmp( pcLine , "c" ) ) {
268 if( m_bIsRunning ) {
269 (*m_pEngineRef)->cancel();
270 m_aDebugCondition.reset();
271 }
272 else fprintf( stderr,"no script running !\n" );
273 }
274 else if( !strcmp( pcLine , "q" ) ) {
275 if( m_bIsRunning ) {
276 m_aDebugCondition.reset();
277 (*m_pEngineRef)->cancel();
278 }
279 else {
280 m_bIsTerminating = TRUE;
281 fprintf(stderr, "Debugger terminates\n" );
282 break;
283 }
284 }
285 else if( ! strcmp( pcLine , "id" ) ) {
286
287 XIntrospectionAccessRef ref = (*m_pInvokationRef)->getIntrospection();
288
289 dumpIntrospectionToStream( ref , stderr );
290
291
292 }
293 else if( ! strncmp( pcLine , "idv" , 3) ) {
294 try {
295 UsrAny any = (*m_pInvokationRef)->getValue( PCHAR_TO_USTRING( &(pcLine[4]) ) );
296 dumpVarToStream( &(pcLine[4]) , any , stderr );
297 }
298 catch(UnknownPropertyException& e ) {
299 fprintf( stderr, "UnknownPropertyException\n" );
300 }
301 catch(IllegalArgumentException& e ) {
302 fprintf( stderr, "IllegalArgumentException\n" );
303 }
304 }
305 else if( !strcmp( pcLine , "t" ) ) {
306 }
307 else if( !strcmp( pcLine , "h" ) ) {
308 fprintf( stderr , "\nvalid commands :\n"
309 "Go : g\n"
310 "StepOver : s\n"
311 "StepIn : si\n"
312 "StepOut : so\n"
313 "Set BreakPoint : sbp Line [ModuleName]\n"
314 "Remove BreakPoint : rbp [Line] [ModuleName]\n"
315 "via XDebugging Interface :\n"
316 " dump Variable : dv varname [CallStack]\n"
317 " set Variable : sv varname value [CallStack]\n"
318 "globals via XInvokation Interface :\n"
319 " dump Global vars : id\n"
320 " dump Variable : idv varname\n"
321 " set Variable : isv varname value\n"
322 "ContextInformation : ci\n"
323 "Dump callstack : d\n"
324 "Cancel : c (stops actual script)\n"
325 "Quit : q (exits debugger)\n"
326 );
327 }
328 else if( ! strlen( pcLine ) ) {
329 }
330 else {
331 fprintf( stderr , "unknown command %s\n" , pcLine );
332 }
333 }
334 }
335
dumpIntrospectionToStream(const XIntrospectionAccessRef & ref,FILE * f)336 void CmdDebugger::dumpIntrospectionToStream( const XIntrospectionAccessRef &ref, FILE *f )
337 {
338 int i,iMax;
339 fprintf( stderr, "Callable Attributes (Methods) :\n" );
340 Sequence<XIdlMethodRef> seq = ref->getMethods( 0 );
341 iMax = seq.getLen();
342 XIdlMethodRef *aRef = seq.getArray();
343 for( i = 0; i < iMax ; i++ ) {
344 fprintf( f, " %s\n" , USTRING_TO_PCHAR( aRef[i]->getName( ) ) );
345 }
346
347 fprintf( stderr, "Other attributes\n" );
348 Sequence<Property> seqProp = ref->getProperties( 0 );
349 iMax = seqProp.getLen();
350 Property *aProp = seqProp.getArray();
351 for( i = 0; i < iMax ; i ++ ) {
352 fprintf( f, " %s %s\n" , USTRING_TO_PCHAR( aProp[i].Type->getName() ),
353 USTRING_TO_PCHAR( aProp[i].Name ) );
354 }
355
356 }
357
dumpVarToStream(const char * pc,const UsrAny & aValue,FILE * f)358 void CmdDebugger::dumpVarToStream( const char *pc , const UsrAny &aValue, FILE *f )
359 {
360 TypeClass type = aValue.getReflection()->getTypeClass();
361
362 if( TypeClass_INT == type ) {
363 fprintf( f, "INT32 %s : %d\n" , pc , aValue.getINT32() );
364 }
365 else if( TypeClass_ENUM == type ) {
366 fprintf( f, "ENUM %s : %d\n", pc , aValue.getEnumAsINT32() );
367 }
368 else if( TypeClass_STRING == type ) {
369 fprintf( f, "STRING %s : %s\n" , pc , USTRING_TO_PCHAR( aValue.getString()) );
370 }
371 else if( TypeClass_BOOLEAN == type ) {
372 fprintf( f, "BOOL %s : %d\n", pc , aValue.getBOOL() );
373 }
374 else if( TypeClass_CHAR == type ) {
375 fprintf( f, "char %s : %d\n", pc , ( INT32) aValue.getChar() );
376 }
377 else if( TypeClass_SHORT == type ) {
378 fprintf( f, "INT16 %s : %d\n", pc , (INT32) aValue.getINT16());
379 }
380 else if( TypeClass_LONG == type ) {
381 fprintf( f, "LONG %s : %d\n", pc , (INT32) aValue.getINT32());
382 }
383 else if( TypeClass_UNSIGNED_SHORT == type ) {
384 fprintf( f, "UINT16 %s : %d\n", pc , (INT32) aValue.getUINT16() );
385 }
386 else if( TypeClass_UNSIGNED_BYTE == type ) {
387 fprintf( f, "Byte %s : %d\n", pc , (INT32) aValue.getBYTE() );
388 }
389 else if( TypeClass_UNSIGNED_INT == type ) {
390 fprintf( f, "UINT32 %s : %d\n", pc , aValue.getUINT32() );
391 }
392 else if( TypeClass_FLOAT == type ) {
393 fprintf( f, "float %s : %f\n" , pc , aValue.getFloat() );
394 }
395 else if( TypeClass_DOUBLE == type ) {
396 fprintf( f, "double %s : %f\n" , pc , aValue.getDouble() );
397 }
398 else if( TypeClass_VOID == type ) {
399 fprintf( f, "void %s :\n" , pc );
400 }
401 else if( TypeClass_INTERFACE == type ) {
402 // Check, what has been put in
403 if( aValue.getReflection() == XPropertySet_getReflection() ) {
404 // XPropertySet !
405 XPropertySetRef *pRef = ( XPropertySetRef * ) aValue.get();
406 XPropertySetInfoRef refInfo = (*pRef)->getPropertySetInfo();
407 Sequence< Property > seq = refInfo->getProperties();
408 int i,iMax = seq.getLen();
409
410 Property *pArray;
411 pArray = seq.getArray();
412 fprintf( stderr, "Property List :\n" );
413 for( i = 0; i < iMax ; i ++ ) {
414 fprintf( f, "%s\t %s\n" , USTRING_TO_PCHAR(pArray[i].Type->getName()),
415 USTRING_TO_PCHAR( pArray[i].Name ) );
416 }
417 }
418 else if( aValue.getReflection() == XInvokation_getReflection() ) {
419 XInvokationRef *pRef = ( XInvokationRef * ) aValue.get();
420 XIntrospectionAccessRef refIntro = (*pRef)->getIntrospection();
421
422 dumpIntrospectionToStream( refIntro, stderr );
423 }
424 }
425 else if( TypeClass_SEQUENCE == type ) {
426 fprintf( f , "%s Sequence \n" , pc );
427 String s( " " );
428 s += pc;
429 SequenceReflection *pSeqRefl = ( SequenceReflection * ) aValue.getReflection();
430
431 int i,iMax = pSeqRefl->getLen( aValue );
432
433 for( i = 0 ; i < iMax ; i ++ ) {
434 dumpVarToStream( s.GetCharStr() , pSeqRefl->get( aValue , i ) , stderr );
435 }
436 }
437 else {
438 fprintf( f, "%s : unknown %d\n" , pc , type );
439 }
440
441 }
442
detach()443 void CmdDebugger::detach()
444 {
445 OSL_ASSERT( m_pDebuggingRef );
446
447 m_bIsRunning = FALSE;
448 m_pDebuggingRef = 0;
449 m_pEngineRef = 0;
450 m_pInvokationRef = 0;
451 }
452
453 // Methoden von XInterface
queryInterface(Uik aUik,XInterfaceRef & rOut)454 BOOL CmdDebugger::queryInterface( Uik aUik, XInterfaceRef & rOut )
455 {
456 if( aUik == XEngineListener::getSmartUik() )
457 rOut = (XEngineListener*)this;
458 else
459 return OWeakObject::queryInterface( aUik, rOut );
460 return TRUE;
461 }
462
463
464
465
466
467
468 /*
469 * main.
470 */
main(int argc,char ** argv)471 int __LOADONCALLAPI main (int argc, char **argv)
472 {
473 XMultiServiceFactoryRef xSMgr = createRegistryServiceManager();
474 registerUsrServices( xSMgr );
475 setProcessServiceManager( xSMgr );
476
477 XInterfaceRef x = xSMgr->createInstance( L"stardiv.uno.repos.ImplementationRegistration" );
478 XImplementationRegistrationRef xReg( x, USR_QUERY );
479 sal_Char szBuf[1024];
480
481 ORealDynamicLoader::computeModuleName( "pythonengine", szBuf, 1024 );
482 UString aDllName( StringToOUString( szBuf, CHARSET_SYSTEM ) );
483 xReg->registerImplementation( L"stardiv.loader.SharedLibrary", aDllName, XSimpleRegistryRef() );
484
485 ORealDynamicLoader::computeModuleName( "aps", szBuf, 1024 );
486 aDllName = UString( StringToOUString( szBuf, CHARSET_SYSTEM ) );
487 xReg->registerImplementation( L"stardiv.loader.SharedLibrary", aDllName, XSimpleRegistryRef() );
488
489 XInterfaceRef y = xSMgr->createInstance( L"stardiv.script.Python" );
490 XEngineRef yEngine( y, USR_QUERY );
491
492 x = xSMgr->createInstance( L"stardiv.script.Python" );
493 XEngineRef xEngine( x, USR_QUERY );
494
495
496 UString Script;
497
498 Sequence<UsrAny> args(3);
499 UsrAny *pArray = args.getArray();
500 pArray[0].setString( L"Arg_0" );
501 pArray[1].setString( L"Arg_1" );
502 pArray[2].setString( L"Arg_2" );
503
504 if( argc > 2) {
505 Script = StringToOUString( String( argv[2] ) , CHARSET_DONTKNOW );
506 }
507
508 XInvokationRef xInvokation( x , USR_QUERY );
509 XDebuggingRef xDebug( x , USR_QUERY );
510
511 CmdDebugger *pDbg = new CmdDebugger( &xDebug , &xEngine , &xInvokation );
512
513 XEngineListenerRef xDebugRef( (XEngineListener *) pDbg , USR_QUERY);
514 xEngine->addEngineListener( xDebugRef );
515
516
517 if( argc >1 && ! strcmp( argv[1] , "1" ) ) {
518 fprintf( stderr, "one thread only\n" );
519 Script = UString( L"print 'Hello World'\n" );
520 xEngine->runAsync( Script , XInterfaceRef(), args , XEngineListenerRef() );
521 }
522 else if( argc >1 && ! strcmp( argv[1] , "2" ) ) {
523
524 xEngine->runAsync( UString( L"x=1\nprint 1\n") , XInterfaceRef(), args , XEngineListenerRef() );
525 xEngine->runAsync( UString( L"x=x+1\nprint 2\n") , XInterfaceRef(), args , XEngineListenerRef() );
526 xEngine->runAsync( UString( L"x=x+1\nprint 3\n") , XInterfaceRef(), args , XEngineListenerRef());
527 xEngine->runAsync( UString( L"x=x+1\nprint 4\n") , XInterfaceRef(), args , XEngineListenerRef() );
528
529
530
531 }
532 else if( argc >1 && ! strcmp( argv[1] , "3" ) ) {
533
534 fprintf( stderr , "1st thread in engine y, next 5 threads in engine x\n" );
535 yEngine->runAsync( UString( L"print 1\n") , XInterfaceRef(), args , XEngineListenerRef() );
536 xEngine->runAsync( UString( L"print 2\n") , XInterfaceRef(), args , XEngineListenerRef() );
537 xEngine->runAsync( UString( L"print 3\n") , XInterfaceRef(), args , XEngineListenerRef() );
538 xEngine->runAsync( UString( L"print 4\n") , XInterfaceRef(), args , XEngineListenerRef());
539 xEngine->runAsync( UString( L"print 5\n") , XInterfaceRef(), args , XEngineListenerRef());
540 xEngine->runAsync( UString( L"print 6\n") , XInterfaceRef(), args , XEngineListenerRef());
541
542
543 }
544 pDbg->cmdLine();
545
546 xEngine->removeEngineListener( xDebugRef );
547
548 xReg->revokeImplementation( aDllName, XSimpleRegistryRef() );
549
550 fprintf( stderr, "main terminates\n" );
551 return 0;
552 }
553
554