Debian Codegraph @

tracing.c

raw
1 /*
2 * The Regina Rexx Interpreter
3 * Copyright (C) 1992-1994 Anders Christensen <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "rexx.h"
21 #include <stdio.h>
22 #ifdef HAVE_UNISTD_H
23 # include <unistd.h>
24 #endif
25 #ifdef HAVE_ASSERT_H
26 # include <assert.h>
27 #endif
28 #include <string.h>
29
30
31 typedef struct { /* tra_tsd: static variables of this module (thread-safe) */
32 int traceflag; /* boolean: 1 indicates that trace output is NOT displayed */
33 int lasttracedline;
34 int intercount; /* number of times to execute trace without interaction */
35 int quiet; /* boolean: run quietly in interaction trace */
36 int notnow;
37 char tracestr[BUFFERSIZE+100];
38 char buf0[32];
39 int bufptr0;
40 char tracefmt[50];
41 int initialhtmltracing; /* set to 1 after first line of HTML tracing set */
42 } tra_tsd_t; /* thread-specific but only needed by this module. see
43 * init_tracing
44 */
45
46 PROTECTION_VAR( trace_setting )
47
48 /* init_tracing initializes the module.
49 * Currently, we set up the thread specific data.
50 * The function returns 1 on success, 0 if memory is short.
51 */
52 int init_tracing( tsd_t *TSD )
53 {
54 tra_tsd_t *tt;
55
56 if ( TSD->tra_tsd != NULL )
57 return 1;
58
59 if ( ( TSD->tra_tsd = MallocTSD( sizeof( tra_tsd_t ) ) ) == NULL )
60 return 0;
61 tt = (tra_tsd_t *)TSD->tra_tsd;
62 memset( tt, 0, sizeof( tra_tsd_t ) );
63 tt->lasttracedline = -1;
64 return 1;
65 }
66
67 int purge_tracing( tsd_t *TSD )
68 {
69 if ( TSD->tra_tsd )
70 {
71 FreeTSD( TSD->tra_tsd );
72 TSD->tra_tsd = NULL;
73 }
74 return 0;
75 }
76
77 int pushcallstack( const tsd_t *TSD, treenode *thisptr )
78 {
79 nodeptr *tmpptr;
80 tra_tsd_t *tt;
81
82 tt = (tra_tsd_t *)TSD->tra_tsd;
83 if ( TSD->systeminfo->cstackcnt >= TSD->systeminfo->cstackmax )
84 {
85 assert( TSD->systeminfo->cstackcnt == TSD->systeminfo->cstackmax );
86 tmpptr = (nodeptr *)MallocTSD( ( TSD->systeminfo->cstackmax * 2 + 10 ) *
87 sizeof( nodeptr ) );
88 if ( TSD->systeminfo->callstack )
89 {
90 memcpy( tmpptr, TSD->systeminfo->callstack,
91 TSD->systeminfo->cstackcnt * sizeof( nodeptr ) );
92 FreeTSD( TSD->systeminfo->callstack );
93 }
94 TSD->systeminfo->callstack = tmpptr;
95 TSD->systeminfo->cstackmax *= 2;
96 TSD->systeminfo->cstackmax += 10;
97 }
98
99 TSD->systeminfo->callstack[TSD->systeminfo->cstackcnt++] = thisptr;
100 return TSD->systeminfo->cstackcnt;
101 }
102
103 void popcallstack( const tsd_t *TSD, int value )
104 {
105 tra_tsd_t *tt;
106
107 tt = (tra_tsd_t *)TSD->tra_tsd;
108 if ( value >= 0 )
109 {
110 assert( TSD->systeminfo->cstackcnt >= value );
111 TSD->systeminfo->cstackcnt = value;
112 }
113
114 --TSD->systeminfo->cstackcnt;
115
116 assert( TSD->systeminfo->cstackcnt >= 0 );
117 }
118
119 static void printout( tsd_t *TSD, const streng *message )
120 {
121 int rc;
122 FILE *fp=stderr;
123 FILE *myfp=NULL;
124 char *rxtrace=NULL;
125 tra_tsd_t *tt;
126
127 tt = (tra_tsd_t *)TSD->tra_tsd;
128
129 rc = HOOK_GO_ON;
130 if ( TSD->systeminfo->hooks & HOOK_MASK( HOOK_STDERR ) )
131 rc = hookup_output( TSD, HOOK_STDERR, message );
132
133 if ( rc == HOOK_GO_ON )
134 {
135 if ( get_options_flag( TSD->currlevel, EXT_STDOUT_FOR_STDERR ) )
136 fp = stdout;
137 rxtrace = getenv( "RXTRACE" );
138 if ( rxtrace != NULL )
139 {
140 myfp = fopen( rxtrace, "a" );
141 if ( myfp != NULL )
142 fp = myfp;
143 }
144 if ( get_options_flag( TSD->currlevel, EXT_TRACE_HTML ) )
145 {
146 if ( tt->initialhtmltracing == 0 )
147 {
148 tt->initialhtmltracing = 1;
149 fwrite( "Content-Type: text/html\n\n", 25, 1, fp );
150 }
151 fwrite( "<FONT COLOR=#669933><PRE>", 25, 1, fp );
152 }
153 fwrite( message->value, message->len, 1, fp ) ;
154 if ( get_options_flag( TSD->currlevel, EXT_TRACE_HTML ) )
155 fwrite( "</PRE></FONT>", 13, 1, fp );
156 #if defined(DOS) || defined(OS2) || defined(WIN32)
157 /*
158 * stdout is open in binary mode, so we need to add the
159 * extra CR to the end of the line.
160 */
161 fputc( REGINA_CR, fp );
162 #endif
163 fputc( REGINA_EOL, fp );
164 fflush( fp );
165 if ( myfp )
166 fclose( fp );
167 }
168 }
169
170 /*
171 * Only called if return code from command to environment is NOT zero
172 */
173 void traceerror( tsd_t *TSD, const treenode *thisptr, int RC )
174 {
175 streng *message;
176 /* Fix for Bug #434; ANSI 8.3.5 */
177 if ( ( TSD->trace_stat == 'E' ) || ( RC < 0 && ( TSD->trace_stat == 'F' || TSD->trace_stat == 'N' ) ) )
178 {
179 traceline( TSD, thisptr, 'C', 0 );
180 message = Str_makeTSD( 20 + sizeof( int ) * 3 ) ;
181 message->len = sprintf( message->value, " +++ RC=%d +++", RC );
182
183 printout( TSD, message );
184 Free_stringTSD( message );
185 }
186 }
187
188 void tracecompound( tsd_t *TSD, const streng *stem, int length,
189 const streng *index, char trch )
190 {
191 streng *message;
192 tra_tsd_t *tt;
193 int indent;
194
195 tt = (tra_tsd_t *)TSD->tra_tsd;
196 if ( tt->traceflag || ( TSD->trace_stat != 'I' ) || tt->quiet )
197 return;
198
199 indent = TSD->systeminfo->cstackcnt + TSD->systeminfo->ctrlcounter;
200 message = Str_makeTSD( stem->len + index->len + 30 + indent );
201
202 sprintf( tt->tracestr, " >%c> %%%ds \"%%.%ds.%%.%ds\"",
203 trch, indent, length, index->len );
204 message->len = sprintf( message->value, tt->tracestr,
205 "", stem->value, index->value );
206
207 printout( TSD, message );
208 Free_stringTSD( message );
209 }
210
211 void starttrace( const tsd_t *TSD )
212 {
213 tra_tsd_t *tt;
214
215 tt = (tra_tsd_t *)TSD->tra_tsd;
216 tt->traceflag = 0;
217 tt->notnow = 1;
218 }
219
220 static void tracemsg( tsd_t *TSD )
221 {
222 streng *message;
223 const streng *msg;
224
225 msg = errortext( TSD, 0, 3, 0, 0 );
226 message = Str_makeTSD( 12 + Str_len( msg ) );
227 Str_catstrTSD( message, " +++ " );
228 Str_catTSD( message, msg );
229 printout( TSD, message );
230 Free_stringTSD( message );
231 }
232
233 int intertrace( tsd_t *TSD )
234 {
235 streng *str=NULL;
236 int retvalue1,rc;
237 tra_tsd_t *tt;
238
239 tt = (tra_tsd_t *)TSD->tra_tsd;
240
241 if ( tt->intercount )
242 {
243 tt->intercount -= 1;
244 if ( tt->intercount == 0 )
245 {
246 tt->quiet = 0;
247 tt->traceflag = 0;
248 }
249 else
250 return 0;
251 }
252
253 if ( tt->traceflag )
254 return 0;
255
256 if ( tt->notnow == 1 )
257 {
258 tt->notnow = 2;
259 return 0;
260 }
261 else if ( tt->notnow == 2 )
262 {
263 tt->notnow = 0;
264 tracemsg( TSD );
265 }
266 tt->traceflag = 1;
267 retvalue1 = -1;
268
269 for ( ; retvalue1 < 0; )
270 {
271 rc = HOOK_GO_ON;
272 if ( TSD->systeminfo->hooks & HOOK_MASK( HOOK_TRCIN ) )
273 rc = hookup_input( TSD, HOOK_TRCIN, &str );
274
275 if ( rc == HOOK_GO_ON )
276 str = readkbdline( TSD );
277
278 if ( str->len == 0 )
279 {
280 tt->traceflag = 0;
281 retvalue1 = 0;
282 }
283
284 if ( ( Str_len( str ) == 1 ) && (str->value[0] == '=' ) )
285 {
286 tt->traceflag = 0;
287 retvalue1 = 1;
288 }
289 else if ( str->len )
290 {
291 dointerpret( TSD, str );
292 if ( !TSD->systeminfo->interactive )
293 {
294 tt->intercount = tt->quiet = 0;
295 return 0;
296 }
297 if ( tt->intercount )
298 {
299 if ( tt->quiet )
300 tt->traceflag = 1;
301 else
302 tt->traceflag = 0;
303 return 0;
304 }
305 }
306 }
307
308 return retvalue1;
309 }
310
311 void tracenumber( tsd_t *TSD, const num_descr *num, char type )
312 {
313 char tmpch;
314 streng *message,*tmpstr;
315 tra_tsd_t *tt;
316 num_descr nd;
317 int indent;
318
319 tmpch = TSD->currlevel->tracestat;
320 if ( ( tmpch != 'I' ) && ( tmpch != 'R' ) )
321 return;
322
323 tt = (tra_tsd_t *)TSD->tra_tsd;
324 if ( tt->traceflag || tt->quiet )
325 return;
326
327 memset( &nd, 0, sizeof( num_descr ) );
328 descr_copy( TSD, num, &nd );
329 tmpstr = str_norm( TSD, &nd, NULL );
330 indent = TSD->systeminfo->cstackcnt + TSD->systeminfo->ctrlcounter;
331 message = Str_makeTSD( 30 + indent + tmpstr->len );
332 sprintf( tt->tracestr, " >%%c> %%%ds \"%%.%ds\"",
333 indent, tmpstr->len );
334 message->len = sprintf( message->value, tt->tracestr,
335 type, "", tmpstr->value );
336 printout( TSD, message );
337 if ( nd.num != NULL )
338 FreeTSD( nd.num );
339 Free_stringTSD( message );
340 Free_stringTSD( tmpstr );
341 }
342
343 void tracebool( tsd_t *TSD, int value, char type )
344 {
345 char tmpch;
346 streng *message;
347 tra_tsd_t *tt;
348 int indent;
349
350 tmpch = TSD->currlevel->tracestat;
351 if ( ( tmpch != 'I' ) && ( tmpch != 'R' ) )
352 return;
353
354 tt = (tra_tsd_t *)TSD->tra_tsd;
355 if ( tt->traceflag || tt->quiet )
356 return;
357
358 indent = TSD->systeminfo->cstackcnt + TSD->systeminfo->ctrlcounter;
359 message = Str_makeTSD( 35 + indent );
360 sprintf( tt->tracestr, " >%%c> %%%ds \"%%d\"",
361 indent );
362 message->len = sprintf( message->value, tt->tracestr,
363 type, "", value );
364 printout( TSD, message );
365 Free_stringTSD( message );
366 }
367
368 void tracevalue( tsd_t *TSD, const streng *str, char type )
369 {
370 char tmpch;
371 streng *message;
372 tra_tsd_t *tt;
373 int indent,i;
374
375 /*
376 * ANSI 8.3.17 requires placeholders in PARSE to be traced with TRACE R
377 */
378 tmpch = TSD->currlevel->tracestat;
379 if ( ( tmpch != 'I' ) && ( tmpch != 'R' ) )
380 return;
381
382 tt = (tra_tsd_t *)TSD->tra_tsd;
383 if ( tt->traceflag || tt->quiet )
384 return;
385
386 indent = TSD->systeminfo->cstackcnt + TSD->systeminfo->ctrlcounter;
387 message = Str_makeTSD( str->len + 30 + indent );
388 sprintf( tt->tracestr, " >%c> %%%ds \"", type, indent );
389 message->len = sprintf( message->value, tt->tracestr, "" );
390 /* bug #279 if we encounter a nul value in the value to be displayed, show a space instead */
391 for (i=0;i<str->len;i++)
392 {
393 if ( str->value[i] == '\0' )
394 message->value[message->len++] = ' ';
395 else
396 message->value[message->len++] = str->value[i];
397 }
398 message->value[message->len++] = '"';
399
400 printout( TSD, message );
401 Free_stringTSD( message );
402 }
403
404 void traceline( tsd_t *TSD, const treenode *thisptr, char tch, int offset )
405 {
406 streng *srcstr;
407 streng *message;
408 tra_tsd_t *tt;
409 int indent;
410
411 tt = (tra_tsd_t *)TSD->tra_tsd;
412 if ( tt->traceflag || tt->quiet )
413 return;
414
415 if ( ( thisptr->charnr < 0 ) || ( thisptr->lineno < 0 ) )
416 return;
417
418 switch ( tch )
419 {
420 case 'I':
421 case 'R':
422 case 'A':
423 break; /* Oh yes, break the IRA ;-) */
424
425 case 'L':
426 if ( thisptr->type == X_LABEL )
427 break;
428 return;
429
430 case 'C':
431 if ( ( thisptr->type == X_COMMAND )
432 || ( ( thisptr->type == X_ADDR_N ) && thisptr->p[0] ) )
433 break;
434 return;
435
436 default:
437 return;
438 }
439
440 srcstr = getsourceline( TSD, thisptr->lineno, thisptr->charnr,
441 &TSD->systeminfo->tree );
442
443 indent = TSD->systeminfo->cstackcnt + TSD->systeminfo->ctrlcounter;
444 message = Str_makeTSD( indent + 20 + srcstr->len + offset );
445 if ( thisptr->lineno == tt->lasttracedline )
446 {
447 sprintf( tt->tracestr, " *-* %%%ds%%.%ds",
448 indent + offset, srcstr->len );
449 message->len = sprintf( message->value, tt->tracestr,
450 "", srcstr->value );
451 }
452 else
453 {
454 sprintf( tt->tracestr, "%%6d *-* %%%ds%%.%ds",
455 indent + offset, srcstr->len );
456 message->len = sprintf( message->value, tt->tracestr,
457 thisptr->lineno, "", srcstr->value );
458 }
459
460 printout( TSD, message );
461 tt->lasttracedline = thisptr->lineno;
462 Free_stringTSD( message );
463 Free_stringTSD( srcstr );
464 }
465
466 void traceback( tsd_t *TSD )
467 {
468 sysinfo ss;
469 streng *message;
470 streng *srcline;
471 nodeptr ptr;
472 int i,j,linesize=128,indent;
473 tra_tsd_t *tt;
474
475 tt = (tra_tsd_t *)TSD->tra_tsd;
476 /*
477 * Allocate enough space for one line and control stuff. Count below
478 * characters for the needed size. Beware of the computed format
479 * entry.
480 */
481 indent = TSD->systeminfo->cstackcnt + TSD->systeminfo->ctrlcounter;
482 message = Str_makeTSD( linesize + indent * 3 + 20 );
483 if ( TSD->currentnode )
484 {
485 srcline = getsourceline( TSD,
486 TSD->currentnode->lineno,
487 TSD->currentnode->charnr,
488 &TSD->systeminfo->tree );
489 if ( srcline->len > linesize )
490 {
491 Free_stringTSD( message );
492 linesize = srcline->len;
493 message = Str_makeTSD( linesize + indent * 3 + 20 );
494 }
495 sprintf( tt->tracefmt, "%%6d +++ %%%ds%%.%ds",
496 indent * 3, srcline->len );
497 message->len = sprintf( message->value, tt->tracefmt,
498 TSD->currentnode->lineno, "",
499 srcline->value );
500 printout( TSD, message );
501 Free_stringTSD( srcline );
502 }
503
504 j = indent;
505 for ( ss = TSD->systeminfo; ss; ss = ss->previous )
506 {
507 for ( i = ss->cstackcnt - 1; i >= 0; i-- )
508 {
509 ptr = ss->callstack[i];
510 if ( !ptr )
511 continue;
512
513 srcline = getsourceline( TSD,
514 ptr->lineno,
515 ptr->charnr,
516 &ss->tree );
517 if ( srcline->len > linesize )
518 {
519 Free_stringTSD( message );
520 linesize = srcline->len;
521 message = Str_makeTSD( linesize + indent * 3 + 20 );
522 }
523
524 j--;
525 if ( ( j > 12 ) && get_options_flag( TSD->currlevel, EXT_PRUNE_TRACE ) )
526 sprintf( tt->tracefmt, "%%6d +++ [...] %%%ds%%.%ds",
527 30, srcline->len );
528 else
529 sprintf( tt->tracefmt, "%%6d +++ %%%ds%%.%ds",
530 j * 3, srcline->len );
531 message->len = sprintf( message->value, tt->tracefmt,
532 ptr->lineno, "",
533 srcline->value );
534 printout( TSD, message );
535 Free_stringTSD( srcline );
536 }
537 }
538 Free_stringTSD( message );
539 }
540
541 /*
542 * Get or show call stack
543 * If stem is NULL, display to stderr
544 */
545 void getcallstack( tsd_t *TSD, streng *stem )
546 {
547 sysinfo ss;
548 nodeptr ptr;
549 int i,j=0;
550 streng *varname=NULL, *value=NULL, *lineno=NULL;
551 int stemlen=0 ;
552 char *eptr=NULL ;
553 streng *tmpptr=NULL ;
554
555 if ( stem )
556 {
557 varname = Str_makeTSD( (stemlen=stem->len) + 8 ) ;
558 memcpy( varname->value, stem->value, stemlen ) ;
559 mem_upper( varname->value, stemlen );
560 eptr = varname->value + stemlen ;
561
562 if (*(eptr-1)!='.')
563 {
564 *((eptr++)-1) = '.' ;
565 stemlen++ ;
566 }
567 }
568 for ( ss = TSD->systeminfo; ss; ss = ss->previous )
569 {
570 for ( i = ss->cstackcnt - 1; i >= 0; i-- )
571 {
572 ptr = ss->callstack[i];
573 if ( !ptr )
574 continue;
575 if ( !ptr->name )
576 continue;
577 tmpptr = ptr->name;
578 /* get the value; lineno name */
579 lineno = int_to_streng( TSD, ptr->lineno );
580 if ( stem )
581 {
582 value = Str_makeTSD( (tmpptr->len) + 10 ) ; /* should not be more than 999999999 levels in the call stack */
583 memcpy( value->value, lineno->value, lineno->len ) ;
584 value->len = lineno->len;
585 Str_catstr_TSD( TSD, value, " " );
586 Str_cat_TSD( TSD, value, tmpptr );
587 Free_stringTSD( lineno );
588 /* set the tail value */
589 sprintf(eptr, "%d", ++j ) ;
590 varname->len = strlen( varname->value ) ;
591 setvalue( TSD, varname, value, -1 ) ;
592 }
593 else
594 {
595 fprintf( stderr, "%.*s %.*s\n", lineno->len, lineno->value, tmpptr->len, tmpptr->value );
596 }
597 }
598 }
599 if ( stem )
600 {
601 *eptr = '0' ;
602 varname->len = stemlen+1 ;
603 tmpptr = int_to_streng( TSD, j ) ;
604 setvalue( TSD, varname, tmpptr, -1 ) ;
605 Free_stringTSD( varname );
606 }
607 }
608
609 void queue_trace_char( const tsd_t *TSD, char ch2 )
610 {
611 tra_tsd_t *tt;
612
613 tt = (tra_tsd_t *)TSD->tra_tsd;
614 if ( tt->bufptr0 < 32 )
615 tt->buf0[tt->bufptr0++] = ch2;
616 else
617 exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__,
618 "too many tracechars queued" );
619 }
620
621 void flush_trace_chars( tsd_t *TSD )
622 {
623 int cnt;
624 tra_tsd_t *tt;
625
626 tt = (tra_tsd_t *)TSD->tra_tsd;
627 for ( cnt = 0; cnt < tt->bufptr0; cnt++ )
628 set_trace_char( TSD, tt->buf0[cnt] );
629
630 tt->bufptr0 = 0;
631 }
632
633 void set_trace_char( tsd_t *TSD, char ch2 )
634 {
635 ch2 = (char) rx_toupper( ch2 );
636 switch ( ch2 )
637 {
638 case '?':
639 TSD->systeminfo->interactive = !TSD->systeminfo->interactive;
640 TSD->currlevel->traceint = (char) TSD->systeminfo->interactive;
641 if ( TSD->systeminfo->interactive )
642 starttrace( TSD );
643 break ;
644
645 case 'A':
646 case 'C':
647 case 'E':
648 case 'F':
649 case 'I':
650 case 'L':
651 case 'N':
652 case 'O':
653 case 'R':
654 TSD->currlevel->tracestat = ch2;
655 break;
656
657 default:
658 exiterror( ERR_INVALID_TRACE, 1, "ACEFILNOR", ch2 );
659 }
660
661 if ( ch2 == 'O' )
662 TSD->systeminfo->interactive = TSD->currlevel->traceint = 0;
663 TSD->trace_stat = TSD->currlevel->tracestat;
664 }
665
666 void set_trace( tsd_t *TSD, const streng *setting )
667 {
668 int cptr,error;
669 tra_tsd_t *tt;
670
671 THREAD_PROTECT( trace_setting )
672
673 if ( myisnumber( TSD, setting ) )
674 {
675 cptr = streng_to_int( TSD, setting, &error );
676 if ( error )
677 exiterror( ERR_INVALID_INTEGER, 7, tmpstr_of( TSD, setting ) );
678
679 /*
680 * If the number is positive, interactive tracing continues
681 * for the supplied number of clauses, but no pausing is done.
682 * If the number is negative, no trace output is inhibited
683 * (as is the pauses) for the supplied number of clauses.
684 * If the number is zero, this is the same as TRACE OFF
685 */
686 tt = (tra_tsd_t *)TSD->tra_tsd;
687 if ( cptr == 0 )
688 {
689 TSD->currlevel->tracestat = 'O';
690 TSD->systeminfo->interactive = 0;
691 TSD->currlevel->traceint = 0;
692 TSD->trace_stat = TSD->currlevel->tracestat;
693 }
694 else if ( cptr > 0 )
695 {
696 tt->quiet = 0;
697 tt->intercount = cptr + 1;
698 }
699 else
700 {
701 tt->quiet = 1;
702 tt->intercount = -cptr + 1;
703 }
704 }
705 else
706 {
707 for ( cptr = 0; cptr < Str_len( setting ); cptr++ )
708 {
709 /*
710 * Check each character in the string. Toggle interactive trace for
711 * each ? until there is an alpha character in the string when we
712 * stop processing the string. So if you TRACE '???IGNORE'
713 * and interactive tracing is currently off, this will turn interactive
714 * tracing on (intermediates) and ignore the remaining string 'GNORE'
715 */
716 set_trace_char( TSD, setting->value[cptr] );
717 if ( rx_isalpha( setting->value[cptr] ) )
718 break;
719 }
720 }
721
722 THREAD_UNPROTECT( trace_setting )
723
724 return;
725 }