Evaluate expressions that modify local variables

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Evaluate expressions that modify local variables

Rodrigo Dias Arruda Senra-2
Hello,

I have implemented a Lua debugger embed in my application (SCADA system
for Industrial Automation).
In step-by-step debugging mode, I would like to evaluate a command string
in the context of the current executing Lua function in a given Lua state.

In other words, a user debugging the code "local some_var = 1;
print(some_var)"
would like to execute the expression "some_var = 3" during a debug session
to change the value of that local variable before the next statement in the
debugged code.

My current approach is to load the command (creating a new chunk) in the
Lua state
and invoke it with pcall.
The problem is that I cannot write local variables inside the debugged
target function, because the debug expression is a new chunk.

My question is: How to change local variables (from C code) ?
I read several threads about merging upvalues, and did some experiments
but  my current understanding is that it will not work because
local variables are registers and are not stored in _ENV.

Is this possible ? How ?

Any pointers would be appreciated.
best regards,
Rod Senra

http://rodrigo.senra.nom.br


Reply | Threaded
Open this post in threaded view
|

Re: Evaluate expressions that modify local variables

Tony Finch
Rodrigo Dias Arruda Senra <[hidden email]> wrote:

> My current approach is to load the command (creating a new chunk) in the
> Lua state and invoke it with pcall. The problem is that I cannot write
> local variables inside the debugged target function, because the debug
> expression is a new chunk.
>
> My question is: How to change local variables (from C code) ?

You can probably do this with a special global variable table used by
debugger commands. Set a metatable on it that uses the debug API
(lua_getlocal / lua_setlocal) to manipulate local variables when they
exist.

Tony.
--
f.anthony.n.finch  <[hidden email]>  http://dotat.at/
Fair Isle: Northeasterly 5 to 7, but easterly 4 at first in southeast.
Moderate or rough, occasionally very rough. Rain or showers. Moderate or good.

Reply | Threaded
Open this post in threaded view
|

Re: Evaluate expressions that modify local variables

Michal Kottman
In reply to this post by Rodrigo Dias Arruda Senra-2
On 24 April 2012 17:16, Rodrigo Dias Arruda Senra
<[hidden email]> wrote:
> My question is: How to change local variables (from C code) ?
> I read several threads about merging upvalues, and did some experiments
> but  my current understanding is that it will not work because
> local variables are registers and are not stored in _ENV.
>
> Is this possible ? How ?

Take a look at my attempt here: http://snippets.luacode.org/snippets/title_88

What I've done is create an ENV table, which actually looks up
variables first in the locals, then in global table. It also supports
writes. There are some hacks regarding the function layout during
indexing ("{where = l + 2…"), but at least you can get the idea how to
implement this.

Reply | Threaded
Open this post in threaded view
|

Re: Evaluate expressions that modify local variables

Peng Zhicheng
In reply to this post by Rodrigo Dias Arruda Senra-2
于 2012-4-24 23:16, Rodrigo Dias Arruda Senra 写道:

> Hello,
>
> ...
>
> My question is: How to change local variables (from C code) ?
> I read several threads about merging upvalues, and did some experiments
> but  my current understanding is that it will not work because
> local variables are registers and are not stored in _ENV.
>
> Is this possible ? How ?
>
> Any pointers would be appreciated.
> best regards,
> Rod Senra
>
> http://rodrigo.senra.nom.br
>
>

if I understand correctly, your debugger takes control as a Debug Hook, right ?
if so, the local variable still lives on the stack.
just refer to the debug information of the Proto to see which stack slot it lives in.

however, by only using the public API, this can be done, but not very efficiently.
see the documents of lua_getinfo, lua_getlocal, lua_setlocal, etc.

yet since you are writing some kind of debugger, you may be able to use some inernal APIs,
i.e. the functions annotated with LUAI_FUNC. see luaF_getlocalname in lfunc.c for such examples.


Reply | Threaded
Open this post in threaded view
|

Re: Evaluate expressions that modify local variables

Rodrigo Dias Arruda Senra-2
In reply to this post by Michal Kottman
Em 24/04/2012 13:32, Michal Kottman escreveu:
On 24 April 2012 17:16, Rodrigo Dias Arruda Senra
[hidden email] wrote:
My question is: How to change local variables (from C code) ?
I read several threads about merging upvalues, and did some experiments
but  my current understanding is that it will not work because
local variables are registers and are not stored in _ENV.

Is this possible ? How ?
Take a look at my attempt here: http://snippets.luacode.org/snippets/title_88
Thank you Michal.
I adapted your code and now my debugger is capable of changing local variables.

I also would like to thank  Tony Finch and Peng Zhicheng for their suggestions.
 
best regards,
Rod Senra
http://rodrigo.senra.nom.br