Recent

Author Topic: Can't modify variable value in debugger.  (Read 409 times)

lazer

  • Full Member
  • ***
  • Posts: 204
Can't modify variable value in debugger.
« on: November 01, 2022, 11:33:34 am »
Hi,

I'm a bit confused with the inability to modify a variable value in the debugger.

When stepping throught the code, I select a variable name , select Evaluate/Modify and see it displayed.

I try editing the value directly as shown but nothing happens.  Then I spot way down at the bottom of the window a dropdown list with "new value" in grey.  Yes, that looks like what I need.

I enter the new value and hit the ":=" button and ...... nothing changes.

Then I discover a hover hint which says "Shift-Enter to confirm".  Ah, that must be it.

I type Shift-Enter and ...... nothing changes.

The eval display at the top is unchanged and when I hover the variable in the source code, it still has the same value.



I've found I can change an integer , as expected, but this variable is a pascal string.
Code: Pascal  [Select][+][-]
  1. filename:string[pathlen+1+12];
  2.  

On the console I find this. 
Code: Pascal  [Select][+][-]
  1. ### TCodeToolManager.HandleException: [20170421200105] "identifier not found: filename" at Line=799 Col=5 in "~/laz/pack.pas"
  2.  

Why don't I get an error in the IDE instead of it stupidly ignoring it and doing nothing ?

It seems like there is a problem, either with pascal strings or more likely with user defined variable types.

TIA.

Lazarus 2.3.0 (rev main-2_3-2165-gc63984efd6) FPC 3.3.1 x86_64-linux-gtk2
« Last Edit: November 01, 2022, 11:41:51 am by lazer »

dseligo

  • Hero Member
  • *****
  • Posts: 876
Re: Can't modify variable value in debugger.
« Reply #1 on: November 01, 2022, 12:09:18 pm »
It says here https://wiki.lazarus.freepascal.org/IDE_Window:_Evaluate/Modify:
You should not attempt to modify "managed" types (strings, dynamic array). This will lead to memory corruption.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 8616
  • Debugger - SynEdit - and more
    • wiki
Re: Can't modify variable value in debugger.
« Reply #2 on: November 01, 2022, 12:28:36 pm »
Changing strings is not yet supported. (it's somewhere on the wiki)
(IIRC, you may be able to change a single char s[2], but with side effects)

Originally this is because
- with gdb there was no function calling. With FpDebug we now have function calling, but it has not yet been implemented for "modify".
- Also there is a 2nd problem. The debug-info does currently not always distinguish between string and pchar

Strings are managed types.
If a string is modified, its refcount need to checked, and a unique version needs to be allocated. Also a new string must be allocated if the length increases.
Memory alloc requires function calls.
Actually, in case of strings those calls go to function directly allocating strings.



Changing a char (inside a string): s[2] := 'a'

If the debugger can tell apart if that is a string or pchar (so it can know if this is the 2nd or 3rd char) then...

IIRC the debugger will accept that. But it changes the memory as if it was an "array of char" or "pchar" => that is it will change the memory.
BUT it will not check if the string is unique. It will not "copy on write".

So if you have a 2nd variable to which you assigned the string, and that points to the same memory, then that 2nd var changes too.



String vs Pchar

Currently the complier does not include the difference in the debug info. So the debugger can not know what the data is.

If you use dwarf-3
- on Windows currently it is enough if the unit in question has dwarf 3
- on Linux, all units that have debug info must have dwarf-3 (i.e. there can be other units without debug info, but if other units have debug info, it must be dwarf 3)
Then  the debugger can use "internal details" to tell the difference.
That is, there still is no info, but the compiler kind of uses "slightly different wording to say exactly the same thing". And the debugger can look for that (which means it works with current versions of fpc, and may change in future)

 

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 8616
  • Debugger - SynEdit - and more
    • wiki
Re: Can't modify variable value in debugger.
« Reply #3 on: November 01, 2022, 12:34:26 pm »
It says here https://wiki.lazarus.freepascal.org/IDE_Window:_Evaluate/Modify:
You should not attempt to modify "managed" types (strings, dynamic array). This will lead to memory corruption.

IIRC with gdb, the debugger could not "protect" you. Gdb would just incorrectly make changes, and that could lead to anything.

FpDebug, has more insight. So it *should* not destroy your memory. But it currently silently ignores the request to modify.


lazer

  • Full Member
  • ***
  • Posts: 204
Re: Can't modify variable value in debugger.
« Reply #4 on: November 01, 2022, 12:39:23 pm »
Quote
You should not attempt to modify "managed" types (strings, dynamic array). This will lead to memory corruption.
Then it should be trapped so that it does NOT cause a memory corruptions ( it maybe, I did not find such a problem ).
Quote
(it's somewhere on the wiki)

That does not excuse that it does need handling intelligently in the IDE.

Currently there is not so much as a beep. Just silence and total lack of result.

How about you trap the ":=" button ; shift-enter  handler and do something to inform the user:

" modifying string variables is currently not supported". Maybe even with a link to relevant wiki page.

At least trap the error and do SOMETHING.

/my2c

« Last Edit: November 02, 2022, 09:39:54 am by lazer »

 

TinyPortal © 2005-2018