Recent

Author Topic: Unexpected Evaluate/Modify behavior  (Read 339 times)

440bx

  • Hero Member
  • *****
  • Posts: 1047
Unexpected Evaluate/Modify behavior
« on: May 06, 2019, 10:21:23 pm »
Hello,

The evaluate/modify window does not seem to set the variable to the specified value.

Use the following test program and follow the steps to see the problem:
Code: Pascal  [Select]
  1. {$APPTYPE  CONSOLE}
  2.  
  3. program _BinaryAndBehavior;
  4.  
  5. uses
  6.   Windows
  7.   ;
  8.  
  9. procedure DoIt;
  10. var
  11.   MenuItemState : UINT;
  12.  
  13. begin
  14.   MenuItemState := 0;      { change value using evaluate/modify               }
  15.   //MenuItemState := 8;    { set to this value using evaluate/modify          }
  16.  
  17.   if MenuItemState and MF_CHECKED > 0 then
  18.   begin
  19.     writeln('  ** Inside if  **');
  20.   end;
  21. end;
  22.  
  23. begin
  24.   DoIt;
  25.  
  26.   writeln('Press return/enter to end this program');
  27.   readln;
  28. end.
Follow these steps:

1.  uncomment line 15.  This should cause "  ** inside if **" to print.
2.  comment back line 15
3. place a breakpoint on line 17.
4. when the breakpoint hits, use the evaluate/modify window to change the value of MenuItemState to 8.  This should cause the writeln('  ** inside if **') to execute BUT, it does not.  Yet, even the locals window shows that MenuItemState has the value 8.

end of post.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5515
    • wiki
Re: Unexpected Evaluate/Modify behavior
« Reply #1 on: May 06, 2019, 10:51:23 pm »
I did some tests:

64bit app: works (-O1 and -O0)
32bit app: works only with -O0

While O1 is "debugger friendly" (I might remove that), it is not "debugger perfect". 

O1 runs something called the "peephole optimizer".
And that means, that a value that is already loaded into a cpu register, is not refreshed from memory, between two statements. Yet the debugger only knows the value that is in memory.

This also does not depend on what debugger is used.
The debug info written by fpc (stabs and dwarf) only specify that the value is at location X in memory.

Full asm code would look like
Code: [Select]
  mov 0 => eax
  move eax => mem

  move mem => eax  // peephole removes this line
  and 8, eax
  test eax for 0
 ....


Setup for the debugger: http://wiki.lazarus.freepascal.org/Debugger_Setup


Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5515
    • wiki
Re: Unexpected Evaluate/Modify behavior
« Reply #2 on: May 06, 2019, 10:56:45 pm »
Heck, even fpc -h prints that

Code: [Select]
-O<x>  Optimizations:
      -O-        Disable optimizations
      -O1        Level 1 optimizations (quick and debugger friendly)
      -O2        Level 2 optimizations (-O1 + quick optimizations)
      -O3        Level 3 optimizations (-O2 + slow optimizations)
      -O4        Level 4 optimizations (-O3 + optimizations which might have unexpected side effects)

Well, the resulting exe may in all friendship tell the debugger the one or other white lie....

440bx

  • Hero Member
  • *****
  • Posts: 1047
Re: Unexpected Evaluate/Modify behavior
« Reply #3 on: May 06, 2019, 11:01:01 pm »
I did some tests:

64bit app: works (-O1 and -O0)
32bit app: works only with -O0

While O1 is "debugger friendly" (I might remove that), it is not "debugger perfect".

Thank you Martin.  That explains it.

Yes, I usually have O1 set (it's the default and it is supposed to be "debugger friendly")  A friendly little white lie it turns out. ;)

I'm somewhat surprised that the 64bit would not have the same "peephole optimizer" problem as the 32bit version (I understand they are different but, they are supposed to do the same job, the results should be very similar.)

Thanks again.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5515
    • wiki
Re: Unexpected Evaluate/Modify behavior
« Reply #4 on: May 06, 2019, 11:04:56 pm »
Btw, "eval and modify"

You can not modify managed types. (string/array)
This will lead to crashes or undefined behaviour.

440bx

  • Hero Member
  • *****
  • Posts: 1047
Re: Unexpected Evaluate/Modify behavior
« Reply #5 on: May 06, 2019, 11:22:50 pm »
Btw, "eval and modify"

You can not modify managed types. (string/array)
This will lead to crashes or undefined behaviour.
That is good to know, thank you.  One more reason not to use managed types. :)
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.