home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / sys / runtime / trace.c < prev    next >
C/C++ Source or Header  |  1999-06-05  |  5KB  |  227 lines

  1. /*
  2. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  3. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  4. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  5. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  6. -- this header is kept unaltered, and a notification of the changes is added.
  7. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  8. -- another product.
  9. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  10. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  11. --                       http://www.loria.fr/SmallEiffel
  12. --
  13. */
  14.  
  15. /*
  16.   This file (trace.c) is automatically included when `run_control.no_check' is
  17.   true (ie. all modes ecxept -boost).
  18.   This file comes after no_check.[hc] to implements the -trace flag as well as
  19.   some other printing stuff.
  20. */
  21.  
  22. /* 
  23.    To print object into the trace-stack :
  24. */
  25. void se_prinT0(T0** o) {
  26.   if (*o == NULL) {
  27.     fprintf(SE_ERR,"Void");
  28.   }
  29.   else {
  30.     fprintf(SE_ERR,"#%p",*o);
  31.   }
  32. }
  33. void se_prinT2(T2* o) {
  34.   fprintf(SE_ERR,"%d",*o);
  35. }
  36. void se_prinT3(T3* o) {
  37.   fprintf(SE_ERR,"'%c'",*o);
  38. }
  39. void se_prinT4(T4* o) {
  40.   fprintf(SE_ERR,"%f",((double)*o));
  41. }
  42. void se_prinT5(T5* o) {
  43.   fprintf(SE_ERR,"%f",*o);
  44. }
  45. void se_prinT6(T6* o) {
  46.   if (*o) {
  47.     fprintf(SE_ERR,"true");
  48.   }
  49.   else {
  50.     fprintf(SE_ERR,"false");
  51.   }
  52. }
  53. void se_prinT7(T7** o) {
  54.   if (*o == NULL) {
  55.     fprintf(SE_ERR,"Void");
  56.   }
  57.   else {
  58.     char*storage = (*o)->_storage;
  59.     int count = (*o)->_count;
  60.     int i = 0;
  61.     fprintf(SE_ERR,"\"");
  62.     while (i < count) {
  63.       fprintf(SE_ERR,"%c",storage[i]);
  64.       i++;
  65.     }
  66.     fprintf(SE_ERR,"\"");
  67.   }
  68. }
  69. void se_prinT8(T8* o) {
  70.   if (*o == NULL) {
  71.     fprintf(SE_ERR,"NULL");
  72.   }
  73.   else {
  74.     fprintf(SE_ERR,"POINTER#%p",*o);
  75.   }
  76. }
  77.  
  78. #ifndef SE_TRACE
  79.  
  80. void se_trace(se_dump_stack*ds,int l,int c,int f) {
  81.   ds->l=l;
  82.   ds->c=c;
  83.   ds->f=f;
  84. }
  85.  
  86. # endif
  87.  
  88. #ifdef SE_TRACE
  89. /*
  90.   Basic SmallEiffel step-by-step execution.
  91. */
  92.  
  93. static FILE* se_trace_file = NULL;
  94. static int se_write_trace_flag = 0;
  95. static int se_trace_ready_flag = 0;
  96. static int se_step_by_step_flag = 1;
  97.  
  98. int get_answer(void) {
  99.   int result = 0;
  100.   char c = getc(stdin);
  101.   if (c != '\n') {
  102.     result = c;
  103.   }
  104.   while (c != '\n') {
  105.     c = getc(stdin);
  106.   }
  107.   return result;
  108. }
  109.  
  110. static void sedb_help_command(void) {
  111.   printf("SmallEiffel debugger.\n");
  112.   printf("List of classes of commands:\n");
  113.   printf("   (h) Help.\n");
  114.   printf("   (s) Stack view.\n");
  115.   printf("   (q) Quit.\n");
  116.   printf("   Return to see the current Eiffel source line.");
  117.   printf("   \n");
  118.   printf("Please, feel free to debug or to complete this simple\n");
  119.   printf("step-by-step debugger (see source file in\n");
  120.   printf("\"SmallEiffel/sys/runtime/trace.c\".\n");
  121. }
  122.  
  123. static void sedb_show_source_line(int l,int c,int f) {
  124.   static int f_memo = 0;
  125.   if (p[f] == NULL) {
  126.     printf("line %d column %d of ???\t\t",l,c);
  127.   }
  128.   else {
  129.     FILE *file = fopen(p[f],"r");
  130.     if (file != NULL) {
  131.       int line = 1;
  132.       int column = 1;
  133.       char cc;
  134.       while (line < l) {
  135.     cc = fgetc(file);
  136.     if (cc == '\n') {
  137.       line++;
  138.     }
  139.       }
  140.       cc = fgetc(file);
  141.       while (cc != '\n') {
  142.     if (cc == '\t') {
  143.       printf("        ");
  144.       column+=7;
  145.     }
  146.     else {
  147.       fputc(cc,stdout);
  148.     }
  149.     cc = fgetc(file);
  150.     column++;
  151.       }
  152.       while (column < 72) {
  153.     fputc(' ',stdout);
  154.     column++;
  155.       }
  156.       printf("l%dc%d ",l,c);
  157.       if (f_memo != f) {
  158.     printf(" %s ",p[f]);
  159.       }
  160.       f_memo = f;
  161.       fclose(file);
  162.     }
  163.     else {
  164.       printf("line %d column %d of %s\t\t",l,c,p[f]);
  165.     }
  166.   }
  167. }
  168.  
  169. void se_trace(se_dump_stack*ds,int l,int c,int f) {
  170.   static char cmd_memo = 1;
  171.   static char cmd;
  172.   ds->l=l;
  173.   ds->c=c;
  174.   ds->f=f;
  175.   if (se_trace_ready_flag) {
  176.     if (se_write_trace_flag) {
  177.       fprintf(se_trace_file,"line %d column %d in %s\n",l,c,p[f]);
  178.       fflush(se_trace_file);
  179.     }
  180.   next_command:
  181.     if (se_step_by_step_flag) {
  182.       if (cmd_memo != 0) {
  183.     printf("(sedb) ");
  184.       }
  185.       fflush(stdout);
  186.       cmd = get_answer();
  187.       cmd_memo = cmd;
  188.       if ((cmd == 'h') || (cmd == 'H') || (cmd == '?')) {
  189.     sedb_help_command();
  190.     goto next_command;
  191.       }
  192.       else if ((cmd == 's') || (cmd == 'S')) {
  193.     se_print_run_time_stack();
  194.     goto next_command;
  195.       }
  196.       else if ((cmd == 'q') || (cmd == 'Q')) {
  197.     exit(1);
  198.       }
  199.       else if (cmd == 0) {
  200.     sedb_show_source_line(l,c,f);
  201.       }
  202.       else {
  203.     printf("Unknown command.\nTtype H for help\n");
  204.     goto next_command;
  205.       }
  206.     }
  207.   }
  208.   else {
  209.     se_trace_ready_flag = 1;
  210.     printf("Write the execution trace in \"trace.se\" file (y/n) ? [n]");
  211.     fflush(stdout);
  212.     if (get_answer() == 'y') {
  213.       se_write_trace_flag = 1;
  214.       se_trace_file = fopen("trace.se","w");
  215.     }
  216.     printf("Step-by-step execution (y/n) ? [y]");
  217.     fflush(stdout);
  218.     if (get_answer() == 'n') {
  219.       se_step_by_step_flag = 0;
  220.     }
  221.     else {
  222.     sedb_help_command();
  223.     }
  224.   }
  225. }
  226. # endif
  227.