dumping stack

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

dumping stack

Philipp Kraus
Hello,

can I create a dump of the Lua stack on C ?
I have seen the Lua_debug interfaces, but I don't understand how it works.
I will dump the stack order of the elements eg function arguments, type
of the stack
elements and all global registered data.

Thanks a lot

Phil



Reply | Threaded
Open this post in threaded view
|

Re: dumping stack

Luiz Henrique de Figueiredo
> can I create a dump of the Lua stack on C ?

See http://lua-users.org/lists/lua-l/2009-01/msg00147.html and its thread.

Reply | Threaded
Open this post in threaded view
|

Re: dumping stack

Philipp Kraus

Am 10.12.2012 um 12:41 schrieb Luiz Henrique de Figueiredo:

can I create a dump of the Lua stack on C ?

See http://lua-users.org/lists/lua-l/2009-01/msg00147.html and its thread.


Thanks for the link, I have change the code a little bit to C++.
I iterate over my stack and check the stack element with lua_type, but
I have got on the function type a problem

 case LUA_TFUNCTION      : p_stream << (lua_iscfunction(p_lua.m_lua.get(), i) ? "c" : "lua") << " function : "
                                          lua_Debug l_debug;
                                          lua_getinfo(p_lua.m_lua.get(), ">n", &l_debug);
                                          p_stream << l_debug.name << " (" << l_debug.namewhat << ")";
                                          break;

I would like to read the function name on the stack element i. How can I read the name?
Thanks

Phil

Reply | Threaded
Open this post in threaded view
|

Re: dumping stack

Sven Olsen

I would like to read the function name on the stack element i. How can I read the name?

It looks like you're confusing the C API's stack with the interpreter's runtime stack.  They're different animals :)

The stack exposed in the C API is basically just a mechanism for passing arguments to C functions -- that's the stack you're interacting with when you call lua_iscfunction.  In the context of the C-API's stack, functions don't have names.  They're just pointers.

lua_getinfo, lua_getstack, and luaL_traceback are all tools for analyzing the runtime stack.  They're what you want to use to get a string representation of Lua's execution state. 

-Sven
Reply | Threaded
Open this post in threaded view
|

Re: dumping stack

Philipp Kraus

Am 10.12.2012 um 21:53 schrieb Sven Olsen:


I would like to read the function name on the stack element i. How can I read the name?

It looks like you're confusing the C API's stack with the interpreter's runtime stack.  They're different animals :)

The stack exposed in the C API is basically just a mechanism for passing arguments to C functions -- that's the stack you're interacting with when you call lua_iscfunction.  In the context of the C-API's stack, functions don't have names.  They're just pointers.

Yes I know the difference, but I push a function to the stack first and then its arguments, but on the function push, I use a string name to get the function and I would like to get back this name from the stack item

Phil


Reply | Threaded
Open this post in threaded view
|

Re: dumping stack

Sven Olsen
Yes I know the difference, but I push a function to the stack first and then its arguments, but on the function push, I use a string name to get the function and I would like to get back this name from the stack item

Hrm.  Well, if you push the function via lua_getglobal, then you're pushing it to the C stack -- so you don't want to use lua_getinfo() to get its name back -- lua_getinfo() references the runtime stack.  Instead, you'll need to traverse the global table to find the function's key string.  This is slow, but possible, provided that there's not more than one global name associated with the function.

-Sven
Reply | Threaded
Open this post in threaded view
|

Re: dumping stack

Sven Olsen

Hrm.  Well, if you push the function via lua_getglobal, then you're pushing it to the C stack -- so you don't want to use lua_getinfo() to get its name back -- lua_getinfo() references the runtime stack.  

Actually, that's not quite true :)

If you push various C-functions to the stack, then call lua_call, the function that gets executed via lua_call will become part of the runtime stack, while any other functions pushed as arguments will become part of the new C stack. 

-Sven
Reply | Threaded
Open this post in threaded view
|

Re: dumping stack

Philipp Kraus
Hi Sven,

Am 10.12.2012 um 22:48 schrieb Sven Olsen:


Hrm.  Well, if you push the function via lua_getglobal, then you're pushing it to the C stack -- so you don't want to use lua_getinfo() to get its name back -- lua_getinfo() references the runtime stack.  

Actually, that's not quite true :)

If you push various C-functions to the stack, then call lua_call, the function that gets executed via lua_call will become part of the runtime stack, while any other functions pushed as arguments will become part of the new C stack. 

I'm interested in the C stack only, because for debugging passing / returning arguments from / to the Lua functions. So I push via C API the Lua function to the C stack and run lua_call, but on some debug runs I will create a output
of the C stack before the lua_call is run, so my stack shows eg

4   number 123
3   string "test"
2   boolean "true"
1   lua function

and on the item 1 it would read the name of the function. In my case the Lua function and the arguments of the function are pushed by the routines on program top, so in the cases there are different Lua function.
If I iterate on a stack (eg above), I would get only the function name back. I can determine that a stack item is a lua function (or a C function) but can I get a reference of the name of the function? I push it also with
a name to the stack, so somewhere must be stored that the item n is a lua function with a name or is the stack item of a lua function a pointer to the a memory block?

Thanks

Phil
Reply | Threaded
Open this post in threaded view
|

Re: dumping stack

Jerome Vuarand
2012/12/11 Philipp Kraus <[hidden email]>:

> 4   number 123
> 3   string "test"
> 2   boolean "true"
> 1   lua function
>
> and on the item 1 it would read the name of the function. In my case the Lua
> function and the arguments of the function are pushed by the routines on
> program top, so in the cases there are different Lua function.
> If I iterate on a stack (eg above), I would get only the function name back.
> I can determine that a stack item is a lua function (or a C function) but
> can I get a reference of the name of the function? I push it also with
> a name to the stack, so somewhere must be stored that the item n is a lua
> function with a name or is the stack item of a lua function a pointer to the
> a memory block?

The stack slot 1 is only pointing at the function, it doesn't hold the
name you used to push it there. You can still try to call lua_getinfo
on it. To do that on a function on the C stack, first push the
function on the top of the stack (lua_pushvalue(L, 1) in your case),
then call lua_getinfo with a "what" string that start with the
character ">". See the lua_getinfo manual for more details.

This doesn't guarantee that you will get a function name. I'm not
familiar enough with getinfo to tell the circumstances where you will
or where you won't. I think that usually when you get a function name
in a trace report, the name is infered from the call point, not the
function definition point, so there might be a chance that you cannot
get a proper name in your situation (I don't know if the functions
debug data carry the name used when they were defined). However you
will get a script name and a line number of here the function was
defined, so you can point your user there at the very least.

Reply | Threaded
Open this post in threaded view
|

Re: dumping stack

Philipp Kraus
Hi Jermone,

many thank to you,

Am 11.12.2012 um 15:41 schrieb Jerome Vuarand:
> The stack slot 1 is only pointing at the function, it doesn't hold the
> name you used to push it there. You can still try to call lua_getinfo
> on it. To do that on a function on the C stack, first push the
> function on the top of the stack (lua_pushvalue(L, 1) in your case),
> then call lua_getinfo with a "what" string that start with the
> character ">". See the lua_getinfo manual for more details.

I have tried this, but not with the pushvalue. At the moment I get some
ASCII chars, I think it is the pointer representation.

> This doesn't guarantee that you will get a function name. I'm not
> familiar enough with getinfo to tell the circumstances where you will
> or where you won't. I think that usually when you get a function name
> in a trace report, the name is infered from the call point, not the
> function definition point, so there might be a chance that you cannot
> get a proper name in your situation (I don't know if the functions
> debug data carry the name used when they were defined). However you
> will get a script name and a line number of here the function was
> defined, so you can point your user there at the very least.

I have a set of Lua scripts, that are stored in a database. On another host
system (cluster) a MPI spawned process get the scripts and run the Lua
code some million times. Each script / function is a independend system
in this process. At the moment I'm developing this system, so
I need a debug interface for my Lua codes. I would run the Lua code from C/C++
push values and functions to the stack and read the result.
The information which is needed by this process is "which function is called and
what data is put in / get out", so I need a stack trace during running.
The programm throws exception on error, but I catch the exceptions, log the error
and do the next step. The program need not to break, only the error must be logged
with a trace in user-friendly representation to database.

Do you have got an idea for a good error log creation?
Thanks Phil



Reply | Threaded
Open this post in threaded view
|

Re: dumping stack

Jerome Vuarand
2012/12/11 Philipp Kraus <[hidden email]>:

> I have a set of Lua scripts, that are stored in a database. On another host
> system (cluster) a MPI spawned process get the scripts and run the Lua
> code some million times. Each script / function is a independend system
> in this process. At the moment I'm developing this system, so
> I need a debug interface for my Lua codes. I would run the Lua code from C/C++
> push values and functions to the stack and read the result.
> The information which is needed by this process is "which function is called and
> what data is put in / get out", so I need a stack trace during running.
> The programm throws exception on error, but I catch the exceptions, log the error
> and do the next step. The program need not to break, only the error must be logged
> with a trace in user-friendly representation to database.
>
> Do you have got an idea for a good error log creation?

Depending on how you call your Lua scripts, you may already have the
Lua call trace in the error messages. If that's not the case, you need
to use the last argument to lua_pcall, and have it point to
debug.traceback. If that's not clear enough you need to send us the
part of the code that leads to the lua_pcall, and we can show you how
to modify it to make use of debug.traceback.

The resulting traceback string will contain a line for each call
frame, with the script name, line number and function name. If for
some reason this information is wrong or incomplete in your stack
traces, it may be because of the way you create your scripts. If you
use luaL_loadstring, you might want to consider using luaL_loadbuffer
or lua_load directly, and properly set the "name" argument.

Reply | Threaded
Open this post in threaded view
|

Re: dumping stack

Philipp Kraus

Am 11.12.2012 um 16:31 schrieb Jerome Vuarand:

2012/12/11 Philipp Kraus <[hidden email]>:
I have a set of Lua scripts, that are stored in a database. On another host
system (cluster) a MPI spawned process get the scripts and run the Lua
code some million times. Each script / function is a independend system
in this process. At the moment I'm developing this system, so
I need a debug interface for my Lua codes. I would run the Lua code from C/C++
push values and functions to the stack and read the result.
The information which is needed by this process is "which function is called and
what data is put in / get out", so I need a stack trace during running.
The programm throws exception on error, but I catch the exceptions, log the error
and do the next step. The program need not to break, only the error must be logged
with a trace in user-friendly representation to database.

Do you have got an idea for a good error log creation?

Depending on how you call your Lua scripts, you may already have the
Lua call trace in the error messages. If that's not the case, you need
to use the last argument to lua_pcall, and have it point to
debug.traceback. If that's not clear enough you need to send us the
part of the code that leads to the lua_pcall, and we can show you how
to modify it to make use of debug.traceback.

I create the Lua pointer state, use a own lua_panic function for creating
my exception and load the script with luaL_loadstring. 

My panic function shows:

std::string l_error = lua_tostring(p_lua, -1);
lua_pop(p_lua, 1);
throw exception::RuntimeError(_("Lua error ["+l_error+"]"));

can I get the full stack trace within the panic function?


The resulting traceback string will contain a line for each call
frame, with the script name, line number and function name. If for
some reason this information is wrong or incomplete in your stack
traces, it may be because of the way you create your scripts. If you
use luaL_loadstring, you might want to consider using luaL_loadbuffer
or lua_load directly, and properly set the "name" argument.

Did I understand this correct, that I can get only the stack trace after an error
occurs? I will create a stack trace before and after the Lua script is run for logging
at the moment during developing. I need both a "manual stack trace" if I need it
and a stack trace on errors

Phil


Reply | Threaded
Open this post in threaded view
|

Re: dumping stack

Luiz Henrique de Figueiredo
> I create the Lua pointer state, use a own lua_panic function for creating
> my exception and load the script with luaL_loadstring.

Panic functions are not meant for handling at this level. After a panic,
a Lua state may be unusable. Use load+pcall for handling normal errors in
Lua scripts.

Reply | Threaded
Open this post in threaded view
|

Re: dumping stack

Philipp Kraus

Am 11.12.2012 um 17:31 schrieb Luiz Henrique de Figueiredo:

>> I create the Lua pointer state, use a own lua_panic function for creating
>> my exception and load the script with luaL_loadstring.
>
> Panic functions are not meant for handling at this level. After a panic,
> a Lua state may be unusable. Use load+pcall for handling normal errors in
> Lua scripts.

Okay, but how can I handle runtime errors or user-defined errors?

Reply | Threaded
Open this post in threaded view
|

Re: dumping stack

Luiz Henrique de Figueiredo
> Okay, but how can I handle runtime errors or user-defined errors?

Use lua_pcall.

Reply | Threaded
Open this post in threaded view
|

Re: dumping stack

Sven Olsen
In reply to this post by Philipp Kraus

Do you have got an idea for a good error log creation?

I'd take a look at luaL_traceback() -- though insomuch as your function dispatch is happening from C, you may need to supplement it with data gleaned from the C-stack.

-Sven

PS: I'm sorry for forgetting that lua_getinfo() does, in fact, have a C stack query form.  The runtime form tends to have a lot of more useful string information associated with it, but, I think said info is only well maintained in the case that you're invoking the function from a lua script, rather than via a lua_call().