home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sherlock.zip / WATCH.C < prev    next >
C/C++ Source or Header  |  1994-06-29  |  6KB  |  287 lines

  1. /*
  2. **  Sherlock - Copyright 1992, 1993, 1994
  3. **    Harfmann Software
  4. **    Compuserve: 73147,213
  5. **    All rights reserved
  6. */
  7. /*
  8. **  Watch variables.
  9. */
  10. #include    <stdio.h>
  11. #include    <stdlib.h>
  12. #include    <ctype.h>
  13. #include    <string.h>
  14. #include    <sys\stat.h>
  15. #define     INCL_DOSSESMGR
  16. #define     INCL_DOSEXCEPTIONS
  17. #define     INCL_DOSPROCESS
  18. #include    <os2.h>
  19. #include    "debug.h"
  20.  
  21. #include    "Debugger.h"
  22. #include    "SrcInter.h"
  23. #include    "Source.h"
  24. #include    "Watch.h"
  25.  
  26. /*
  27. ** Typedefs & structures
  28. */
  29. typedef struct _Watchpoint {
  30.     struct _Watchpoint *next;
  31.     DebugModule        *module;
  32.     char           *expr;
  33. } Watchpoint;
  34.  
  35. static Watchpoint *Watchpoints = NULL;
  36.  
  37. /*
  38. ** Evaluate an expression and display the result
  39. */
  40. static void DisplayVariable(char *expr, DebugModule *module, ULONG eip, ULONG ebp)
  41. {
  42. int    rVal;
  43. Value    value;
  44.  
  45.     switch(rVal = evaluate(expr, module, eip, ebp, &value)) {
  46.     case SUCCESS:
  47.         fprintf(logFile, "%s = ", expr);
  48.         switch(value.typeValue) {
  49.         case UNKNOWN_VAL:
  50.             fprintf(logFile, "UNKNOWN!\n");
  51.             break;
  52.         case LONG_VAL:
  53.             fprintf(logFile, "%d (0x%08x)\n", value.val.lVal, value.val.lVal);
  54.             break;
  55.         case DOUBLE_VAL:
  56.             fprintf(logFile, "%lf\n", value.val.dVal);
  57.             break;
  58.         case CHAR_VAL:
  59.             fprintf(logFile, "'%c' (0x%02x)\n", value.val.cVal, value.val.cVal);
  60.             break;
  61.         case STR_VAL:
  62.             fprintf(logFile, "(0x%08x) - \"%s\"\n", value.val.sVal, value.val.sVal);
  63.             break;
  64.         case NAME_VAL:
  65.             fprintf(logFile, "NAME: \"%s\"", value.val.sVal);
  66.             break;
  67.         case PTR_VAL:
  68.             fprintf(logFile, "PTR 0x%08x\n", value.val.lVal);
  69.             break;
  70.         case STRUCT_VAL: {
  71.             StructValue *valueData;
  72.  
  73.             for(valueData = value.val.strVal; valueData;
  74.             valueData = valueData->next) {
  75.             fprintf(logFile, "%s\n", valueData->str);
  76.             }
  77.             for(valueData = value.val.strVal; valueData;) {
  78.             StructValue *killer;
  79.  
  80.             killer = valueData;
  81.             valueData = valueData->next;
  82.             free(killer->str);
  83.             free(killer);
  84.             }
  85.             break;
  86.         }
  87.         default:
  88.             fprintf(logFile, "INTERNAL ERROR!  Type: %d\n", value.typeValue);
  89.             break;
  90.         }
  91.         break;
  92.     case INVALID_VALUE:
  93.         fprintf(logFile, "INVALID VALUE\n");
  94.         break;
  95.     case SYNTAX_ERROR:
  96.         fprintf(logFile, "SYNTAX ERROR\n");
  97.         break;
  98.     case INVALID_NAME:
  99.         fprintf(logFile, "INVALID NAME\n");
  100.         break;
  101.     case NO_MORE_MEMBERS:
  102.         fprintf(logFile, "NO MORE MEMBERS\n");
  103.         break;
  104.     case OUT_OF_CONTEXT:
  105.         fprintf(logFile, "OUT OF CONTEXT\n");
  106.         break;
  107.     case INVALID_INDEX:
  108.         fprintf(logFile, "INVALID INDEX\n");
  109.         break;
  110.     case INTERNAL_ERROR:
  111.         fprintf(logFile, "INTERNAL ERROR\n");
  112.         break;
  113.     default:
  114.         fprintf(logFile, "UNKNOWN ERROR! (%d)\n", rVal);
  115.         break;
  116.     }
  117.     return;
  118. }
  119.  
  120. /*
  121. ** View a variable
  122. */
  123. void ViewVariableCommand(char **ptrs)
  124. {
  125.     /*
  126.     ** Make sure there is a second parameter.
  127.     */
  128.     if(ptrs[0] == NULL) {
  129.     fprintf(logFile, "Please specify a parameter!\n");
  130.         return;
  131.     }
  132.  
  133.     DispatchCommand(DBG_C_ReadReg);
  134.     DisplayVariable(ptrs[0], NULL,
  135.             Linearize(debugBuffer.EIP, debugBuffer.CS),
  136.             Linearize(debugBuffer.EBP, debugBuffer.SS));
  137.     return;
  138. }
  139.  
  140. /*
  141. ** Add a watchpoint to the list of watchpoints.
  142. */
  143. void WatchCommand(char **ptrs)
  144. {
  145. int    i;
  146. Watchpoint *wp;
  147.  
  148.     switch(tolower(ptrs[1][1])) {
  149.  
  150.     /*
  151.     ** Set a watchpoint.
  152.     */
  153.     case 'p':   {
  154.         DebugModule *module;
  155.  
  156.         /*
  157.             ** Find the address of the variable.
  158.             */
  159.         DispatchCommand(DBG_C_ReadReg);
  160.         module = FindModule(debugBuffer.MTE, NULL);
  161.  
  162.             /*
  163.         ** Connect it to the list.
  164.         */
  165.         if(Watchpoints) {
  166.         for(i=0, wp=Watchpoints; wp->next; i++, wp=wp->next) ;
  167.         wp->next = malloc(sizeof(Watchpoint));
  168.         wp = wp->next;
  169.         } else {
  170.         Watchpoints = wp = malloc(sizeof(Watchpoint));
  171.         }
  172.         wp->expr   = strdup(ptrs[0]);
  173.         wp->module = module;
  174.         wp->next   = NULL;
  175.         break;
  176.     }
  177.  
  178.     /*
  179.     ** Clear a watchpoint.
  180.     */
  181.     case 'c': {
  182.         Watchpoint *prior;
  183.         char *dummy;
  184.         int num;
  185.  
  186.             /*
  187.             ** If we want to free all watch points, do it.
  188.             */
  189.             if(strcmp(ptrs[2], "*") == 0) {
  190.                 FreeAllWatchpoints();
  191.                 return;
  192.             }
  193.  
  194.         /*
  195.         ** Find the watch number, and then the watchpoint id.
  196.             */
  197.             i = strtol(ptrs[2], &dummy, 0);
  198.         prior = wp = Watchpoints;
  199.         for(i=0; wp && i<num; i++) {
  200.         prior = wp;
  201.         wp = wp->next;
  202.         }
  203.  
  204.         /*
  205.         ** Make sure the watchpoint exists.
  206.         */
  207.         if(wp == NULL) {
  208.         fprintf(logFile, "ILLEGAL WATCHPOINT NUMBER!\n");
  209.         return;
  210.         }
  211.  
  212.         /*
  213.         ** Remove the watchpoint from the list and from the debuggee.
  214.         */
  215.         if(wp == Watchpoints) {
  216.         Watchpoints = wp->next;
  217.             } else {
  218.                 prior->next = wp->next;
  219.             }
  220.         free(wp->expr);
  221.         free(wp);
  222.         break;
  223.     }
  224.  
  225.     /*
  226.     ** List all of the watchpoints.
  227.     */
  228.     case 'l': {
  229.         for(i=0, wp=Watchpoints; wp; i++, wp=wp->next) {
  230.         fprintf(logFile, "Watchpoint [%d]:(%s)\n",
  231.             i, wp->expr);
  232.         }
  233.         break;
  234.     }
  235.     }
  236. }
  237.  
  238. /*
  239. ** Print the value of all watchpoints.
  240. */
  241. void DumpWatchpoints(void)
  242. {
  243. Watchpoint *wp;
  244. int        i;
  245. ULONG        eip, ebp;
  246.  
  247.  
  248.     /*
  249.     ** Make sure that there are variable to display.
  250.     */
  251.     if(wp == NULL)
  252.     return;
  253.  
  254.     /*
  255.     ** Get the current eip/ebp for the evaluation routine.
  256.     */
  257.     DispatchCommand(DBG_C_ReadReg);
  258.     eip = Linearize(debugBuffer.EIP, debugBuffer.CS);
  259.     ebp = Linearize(debugBuffer.EBP, debugBuffer.SS);
  260.  
  261.     /*
  262.     ** Get the variables and display the values.
  263.     */
  264.     if(Watchpoints)
  265.     fprintf(logFile, "Watched variables\n");
  266.     for(wp = Watchpoints,i=0; wp; wp=wp->next, i++) {
  267.     fprintf(logFile, "[%2d] ", i);
  268.     DisplayVariable(wp->expr, wp->module, eip, ebp);
  269.     }
  270. }
  271.  
  272. /*
  273. ** Free all watchpoints.
  274. */
  275. void FreeAllWatchpoints(void)
  276. {
  277. Watchpoint *wp, *next;
  278.  
  279.     for(wp=Watchpoints; wp; ) {
  280.         next = wp->next;
  281.     free(wp->expr);
  282.         free(wp);
  283.         wp = next;
  284.     }
  285.     Watchpoints = NULL;
  286. }
  287.