C/C++ lua_stackdump lib - feedback welcome

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

C/C++ lua_stackdump lib - feedback welcome

M. Gerhardy
Hi.

I was looking for a way to do stackdumps from within C/C++ - I couldn't find anything that also prints tables.

That's why I did this small helper header file: https://github.com/mgerhardy/lua_stackdump

Feedback is more than welcome. Pull requests, too ;). I'm quite new to lua, so please let me know if you can find anything wrong. There are also a few TODO comments included - if someone could help me to solve them... please do


Kind regards
Martin

PS. the code is public domain.
Reply | Threaded
Open this post in threaded view
|

Re: C/C++ lua_stackdump lib - feedback welcome

Marc Balmer
Hi.

I was looking for a way to do stackdumps from within C/C++ - I couldn't find anything that also prints tables.

That's why I did this small helper header file: https://github.com/mgerhardy/lua_stackdump

Feedback is more than welcome. Pull requests, too ;). I'm quite new to lua, so please let me know if you can find anything wrong. There are also a few TODO comments included - if someone could help me to solve them... please do

Looks like you're new to C/C++ as well... (SCNR).  It is not so common to put code in .h files.  Also, passing around the printing function, is that really needed?

And, overuse of brackets...  And why inline?

Other than that, nice that you put it on guthub.

Reply | Threaded
Open this post in threaded view
|

Re: C/C++ lua_stackdump lib - feedback welcome

M. Gerhardy
I did that to just make this header only - like https://github.com/nothings/stb

the printf is needed for my case, yes, you can stream this to a string buffer, can use different printf functions (like on android, or sdl - just give it a printf compatible function pointer)

the inline was just there to skip the extra extern declaration. but this was added in the meanwhile.

but other than the style that you apparently don't like - any useful feedback? ;)


On Mon, Oct 10, 2016 at 5:22 PM, Marc Balmer <[hidden email]> wrote:
Hi.

I was looking for a way to do stackdumps from within C/C++ - I couldn't find anything that also prints tables.

That's why I did this small helper header file: https://github.com/mgerhardy/lua_stackdump

Feedback is more than welcome. Pull requests, too ;). I'm quite new to lua, so please let me know if you can find anything wrong. There are also a few TODO comments included - if someone could help me to solve them... please do

Looks like you're new to C/C++ as well... (SCNR).  It is not so common to put code in .h files.  Also, passing around the printing function, is that really needed?

And, overuse of brackets...  And why inline?

Other than that, nice that you put it on guthub.




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

Re: C/C++ lua_stackdump lib - feedback welcome

Igor Ehrlich
>And, overuse of brackets...
At this point I got really curious. Could you please be more precise?

On Mon, Oct 10, 2016 at 10:06 PM, M. Gerhardy <[hidden email]> wrote:
I did that to just make this header only - like https://github.com/nothings/stb

the printf is needed for my case, yes, you can stream this to a string buffer, can use different printf functions (like on android, or sdl - just give it a printf compatible function pointer)

the inline was just there to skip the extra extern declaration. but this was added in the meanwhile.

but other than the style that you apparently don't like - any useful feedback? ;)


On Mon, Oct 10, 2016 at 5:22 PM, Marc Balmer <[hidden email]> wrote:
Hi.

I was looking for a way to do stackdumps from within C/C++ - I couldn't find anything that also prints tables.

That's why I did this small helper header file: https://github.com/mgerhardy/lua_stackdump

Feedback is more than welcome. Pull requests, too ;). I'm quite new to lua, so please let me know if you can find anything wrong. There are also a few TODO comments included - if someone could help me to solve them... please do

Looks like you're new to C/C++ as well... (SCNR).  It is not so common to put code in .h files.  Also, passing around the printing function, is that really needed?

And, overuse of brackets...  And why inline?

Other than that, nice that you put it on guthub.




--



--
Best regards,
Igor A. Ehrlich
Reply | Threaded
Open this post in threaded view
|

Re: C/C++ lua_stackdump lib - feedback welcome

Rena
On Mon, Oct 10, 2016 at 3:52 PM, Igor Ehrlich <[hidden email]> wrote:
>And, overuse of brackets...
At this point I got really curious. Could you please be more precise?


I guess technically you don't need the brackets in statements like:

if (depth == 0) {
luasdorintf("%-5i | ", stackIndex);
} else {
luasdorintf("       ", stackIndex);
}

It could be written like:

if (depth == 0)
luasdorintf("%-5i | ", stackIndex);
else
luasdorintf("       ", stackIndex);

but IMO this is a stylistic choice, and I usually prefer the former, as it prevents mistakes like:


if (depth == 0)
luasdorintf("%-5i | ", stackIndex);
else
luasdorintf("       ", stackIndex);
do_another_thing(); //oops, not conditional

Anyway, bikeshedding aside, it's been pointed out on this list before that "public domain" isn't legally recognized in some parts of the world, and you're better to use something like the MIT License (that Lua uses itself) which gives the same effect without any potential hassle for those who want to use it.

--
Sent from my Game Boy.
Reply | Threaded
Open this post in threaded view
|

Re: C/C++ lua_stackdump lib - feedback welcome

Nagaev Boris
On Mon, Oct 10, 2016 at 11:06 PM, Rena <[hidden email]> wrote:

> On Mon, Oct 10, 2016 at 3:52 PM, Igor Ehrlich <[hidden email]> wrote:
>>
>> >And, overuse of brackets...
>> At this point I got really curious. Could you please be more precise?
>>
>
> I guess technically you don't need the brackets in statements like:
>
> if (depth == 0) {
> luasdorintf("%-5i | ", stackIndex);
> } else {
> luasdorintf("       ", stackIndex);
> }
>
> It could be written like:
>
> if (depth == 0)
> luasdorintf("%-5i | ", stackIndex);
> else
> luasdorintf("       ", stackIndex);
>
> but IMO this is a stylistic choice, and I usually prefer the former, as it
> prevents mistakes like:
>
>
> if (depth == 0)
> luasdorintf("%-5i | ", stackIndex);
> else
> luasdorintf("       ", stackIndex);
> do_another_thing(); //oops, not conditional
>
> Anyway, bikeshedding aside, it's been pointed out on this list before that
> "public domain" isn't legally recognized in some parts of the world, and
> you're better to use something like the MIT License (that Lua uses itself)
> which gives the same effect without any potential hassle for those who want
> to use it.
>
> --
> Sent from my Game Boy.

+1 for not omitting { and } in one-line if and else.

--


Best regards,
Boris Nagaev

Reply | Threaded
Open this post in threaded view
|

Re: C/C++ lua_stackdump lib - feedback welcome

Kevin Martin
Have a look at luaprompt. That includes a pretty printer, luap_describe, I incorporated it into the software of the company I used to work for, don't think we ever found an issue.

Thanks,
Kevin

Sent from my iPhone

On 10 Oct 2016, at 21:31, Nagaev Boris <[hidden email]> wrote:

On Mon, Oct 10, 2016 at 11:06 PM, Rena <[hidden email]> wrote:
On Mon, Oct 10, 2016 at 3:52 PM, Igor Ehrlich <[hidden email]> wrote:

And, overuse of brackets...
At this point I got really curious. Could you please be more precise?


I guess technically you don't need the brackets in statements like:

if (depth == 0) {
luasdorintf("%-5i | ", stackIndex);
} else {
luasdorintf("       ", stackIndex);
}

It could be written like:

if (depth == 0)
luasdorintf("%-5i | ", stackIndex);
else
luasdorintf("       ", stackIndex);

but IMO this is a stylistic choice, and I usually prefer the former, as it
prevents mistakes like:


if (depth == 0)
luasdorintf("%-5i | ", stackIndex);
else
luasdorintf("       ", stackIndex);
do_another_thing(); //oops, not conditional

Anyway, bikeshedding aside, it's been pointed out on this list before that
"public domain" isn't legally recognized in some parts of the world, and
you're better to use something like the MIT License (that Lua uses itself)
which gives the same effect without any potential hassle for those who want
to use it.

--
Sent from my Game Boy.

+1 for not omitting { and } in one-line if and else.

--


Best regards,
Boris Nagaev

Reply | Threaded
Open this post in threaded view
|

Re: C/C++ lua_stackdump lib - feedback welcome

Igor Ehrlich
>I guess technically you don't need the brackets in statements like:
>
>if (depth == 0) {
> luasdorintf("%-5i | ", stackIndex);
>} else {
> luasdorintf("       ", stackIndex);
>}
>
>It could be written like:
>
>if (depth == 0)
> luasdorintf("%-5i | ", stackIndex);
>else
> luasdorintf("       ", stackIndex);
>
>but IMO this is a stylistic choice, and I usually prefer the former, as it prevents mistakes like:
>
>
>if (depth == 0)
> luasdorintf("%-5i | ", stackIndex);
>else
> luasdorintf("       ", stackIndex);
> do_another_thing(); //oops, not conditional

Well, as for me, it's not a matter of preferences. I've been facing a lot of issues with this unconditional 'do_another_thing' in somewhat large projects with 5+ developers. So, for me it's not a style, but the guideline. Fun fact, it is not so for linux kernel, as stated in chapter 3 of the linux kernel coding style. I guess that's where this idea comes from mostly. However, for all us non-hackers, the safest approach is the best IMO. Plus, even if you are cautious enough, placing the braces in advance allows to minimize the patch size and layout when adding another statement. This is particularly true for K&R braces, because in this case adding the damn brace will trigger the condition line to be "changed" as well.


On Tue, Oct 11, 2016 at 1:19 AM, Kevin Martin <[hidden email]> wrote:
Have a look at luaprompt. That includes a pretty printer, luap_describe, I incorporated it into the software of the company I used to work for, don't think we ever found an issue.

Thanks,
Kevin

Sent from my iPhone

On 10 Oct 2016, at 21:31, Nagaev Boris <[hidden email]> wrote:

On Mon, Oct 10, 2016 at 11:06 PM, Rena <[hidden email]> wrote:
On Mon, Oct 10, 2016 at 3:52 PM, Igor Ehrlich <[hidden email]> wrote:

And, overuse of brackets...
At this point I got really curious. Could you please be more precise?


I guess technically you don't need the brackets in statements like:

if (depth == 0) {
luasdorintf("%-5i | ", stackIndex);
} else {
luasdorintf("       ", stackIndex);
}

It could be written like:

if (depth == 0)
luasdorintf("%-5i | ", stackIndex);
else
luasdorintf("       ", stackIndex);

but IMO this is a stylistic choice, and I usually prefer the former, as it
prevents mistakes like:


if (depth == 0)
luasdorintf("%-5i | ", stackIndex);
else
luasdorintf("       ", stackIndex);
do_another_thing(); //oops, not conditional

Anyway, bikeshedding aside, it's been pointed out on this list before that
"public domain" isn't legally recognized in some parts of the world, and
you're better to use something like the MIT License (that Lua uses itself)
which gives the same effect without any potential hassle for those who want
to use it.

--
Sent from my Game Boy.

+1 for not omitting { and } in one-line if and else.

--


Best regards,
Boris Nagaev




--
Best regards,
Igor A. Ehrlich
Reply | Threaded
Open this post in threaded view
|

Re: C/C++ lua_stackdump lib - feedback welcome

Thomas Jericke
In reply to this post by Rena
On 10.10.2016 22:06, Rena wrote:

but IMO this is a stylistic choice, and I usually prefer the former, as it prevents mistakes like:


It is almost always preferable, unless you need to store your source code on a floppy disk. If you do the same thing, do it the same way.
Just try to write a code generator an you will see that having a special case for one line 'then' or 'else' statements is simply not a good idea.

It's also against MISRA C rule 15.6.
--
Thomas 
Reply | Threaded
Open this post in threaded view
|

Re: C/C++ lua_stackdump lib - feedback welcome

Philipp Janda
In reply to this post by M. Gerhardy
Am 10.10.2016 um 16:58 schröbte M. Gerhardy:
> Hi.

Hi!

>
> I was looking for a way to do stackdumps from within C/C++ - I couldn't
> find anything that also prints tables.
>
> That's why I did this small helper header file:
> https://github.com/mgerhardy/lua_stackdump
>
> Feedback is more than welcome.

I found that using stack dumps unconditionally can get very confusing --
especially if there is a lot of output and if you use it in multiple
places at the same time or in loops. So I've switch to some form of
stack assertion where I specify what I think the top-most elements of
the stack should look like, and the stack contents only get dumped when
the actual stack contents are different.

What also helps is the source file name, line number, and function name
where the stack dump is from (`__FILE__`, `__LINE__`, and `__func__` (or
`__FUNCTION__` for MSVC)).

>
> Kind regards
> Martin
>

Philipp


p.s.: About the braces thing: I prefer one-statement control structures
without the braces because of vertical space. I use a lot of split
windows and vim buffers, and

     if( something )
     {
       do_something();
     }

is twice as long as the alternative.




Reply | Threaded
Open this post in threaded view
|

Re: C/C++ lua_stackdump lib - feedback welcome

Sean Conner
In reply to this post by M. Gerhardy
It was thus said that the Great M. Gerhardy once stated:

> Hi.
>
> I was looking for a way to do stackdumps from within C/C++ - I couldn't
> find anything that also prints tables.
>
> That's why I did this small helper header file:
> https://github.com/mgerhardy/lua_stackdump
>
> Feedback is more than welcome. Pull requests, too ;). I'm quite new to lua,
> so please let me know if you can find anything wrong. There are also a few
> TODO comments included - if someone could help me to solve them... please do

  Hmm ... and to think I just got by with:

        void dumpstack_lua(lua_State *L)
        {
          int max = max = lua_gettop(L);
          int i;

          /*----------------------------------------------------------
          ; however you want to handle the output---I normally go for
          ; syslog, but for this example, I'm using printf()
          ;---------------------------------------------------------*/
         
          printf("----LUA STACK DUMP----\n");
         
          for (i = 1 ; i <= max ; i++)
          {
            const char *name;
            const char *type;
            int         ri;
           
            lua_getglobal(L,"tostring");
            lua_pushvalue(L,i);
            lua_call(L,1,1);
           
            name = lua_tostring(L,-1);
            type = luaL_typename(L,i);
            ri   = i - max - 1;

            printf("Stack: %d %d - %s %s",i,ri,type,name);
            lua_pop(L,1);
          }
        }
       
  I could probably do more with functions, table and userdata, but for my
own use, this has been good enough.  Also, you could take advantage of Lua
(like I did by calling tostring()) and provide a Lua function to dump
tables, function names, etc.  

  -spc
 

Reply | Threaded
Open this post in threaded view
|

Re: C/C++ lua_stackdump lib - feedback welcome

steve donovan
In reply to this post by Nagaev Boris
On Mon, Oct 10, 2016 at 10:31 PM, Nagaev Boris <[hidden email]> wrote:
> +1 for not omitting { and } in one-line if and else.

Agree. There's a trend in the post-C++ languages to make those
brackets mandatory, and then you don't have to use parens around the
condition.

Writes and reads much better, IHMO

Reply | Threaded
Open this post in threaded view
|

Re: C/C++ lua_stackdump lib - feedback welcome

Marc Balmer
In reply to this post by Igor Ehrlich
>And, overuse of brackets...
At this point I got really curious. Could you please be more precise?

As other mentioned, brackets are not needed for one liners, so I leave them out. I generally leave out what is not needed.


Reply | Threaded
Open this post in threaded view
|

Re: C/C++ lua_stackdump lib - feedback welcome

Marc Balmer
In reply to this post by M. Gerhardy
but other than the style that you apparently don't like - any useful feedback? ;)

Style _is_ important. Even more so if developer work together. Many large projects, e.g. {Open|Net|Free}BSD even have a style guide (which, funny enough, are contradictory in some areas).

Readable code without clutter is easier to maintain if the code base grows. A consistent style helps with that. 
Reply | Threaded
Open this post in threaded view
|

Re: C/C++ lua_stackdump lib - feedback welcome

steve donovan
In reply to this post by Marc Balmer
On Tue, Oct 11, 2016 at 9:26 AM, Marc Balmer <[hidden email]> wrote:
> As other mentioned, brackets are not needed for one liners, so I leave them
> out. I generally leave out what is not needed.

Ah, but the problem is when they become two liners. It's a potential
maintenance problem.

Part of the issue is silly code styles that insist that braces must be
on their own line. (Puts on flame-retardant hat)

Reply | Threaded
Open this post in threaded view
|

Re: C/C++ lua_stackdump lib - feedback welcome

Sean Conner
It was thus said that the Great steve donovan once stated:
> On Tue, Oct 11, 2016 at 9:26 AM, Marc Balmer <[hidden email]> wrote:
> > As other mentioned, brackets are not needed for one liners, so I leave them
> > out. I generally leave out what is not needed.
>
> Ah, but the problem is when they become two liners. It's a potential
> maintenance problem.
>
> Part of the issue is silly code styles that insist that braces must be
> on their own line. (Puts on flame-retardant hat)

  Then my C coding style must really annoy you then 8-P

  -spc (I'll defend my coding style to your death 8-)


Reply | Threaded
Open this post in threaded view
|

Re: C/C++ lua_stackdump lib - feedback welcome

steve donovan
On Tue, Oct 11, 2016 at 10:06 AM, Sean Conner <[hidden email]> wrote:
>   Then my C coding style must really annoy you then 8-P

I once modified an editor so that it could collapse the awful waste of
vertical space that was occurring in some C# I was perusing.

So there's room for all styles in this world ;)

Reply | Threaded
Open this post in threaded view
|

Re: C/C++ lua_stackdump lib - feedback welcome

Sean Conner
It was thus said that the Great steve donovan once stated:
> On Tue, Oct 11, 2016 at 10:06 AM, Sean Conner <[hidden email]> wrote:
> >   Then my C coding style must really annoy you then 8-P
>
> I once modified an editor so that it could collapse the awful waste of
> vertical space that was occurring in some C# I was perusing.

  What?  You are still using a pysical VT-100 in this day and age?  Stuck
with 80x24 text window?

  -spc (Man, I feel sorry for you 8-P


Reply | Threaded
Open this post in threaded view
|

Re: C/C++ lua_stackdump lib - feedback welcome

Enrico Colombini
In reply to this post by steve donovan
On 11-Oct-16 09:32, steve donovan wrote:
> On Tue, Oct 11, 2016 at 9:26 AM, Marc Balmer <[hidden email]> wrote:
>> As other mentioned, brackets are not needed for one liners, so I leave them
>> out. I generally leave out what is not needed.
>
> Ah, but the problem is when they become two liners. It's a potential
> maintenance problem.

I realized that some three decades ago (the hard way) and I considered
braces as mandatory ever since.

> Part of the issue is silly code styles that insist that braces must be
> on their own line. (Puts on flame-retardant hat)

Oh, don't look at my code then ;-)

--
   Enrico

Reply | Threaded
Open this post in threaded view
|

Re: C/C++ lua_stackdump lib - feedback welcome

steve donovan
In reply to this post by Sean Conner
On Tue, Oct 11, 2016 at 10:13 AM, Sean Conner <[hidden email]> wrote:
>   What?  You are still using a pysical VT-100 in this day and age?  Stuck
> with 80x24 text window?

Nah, I'm not one of THEM! But there is only so much space on a monitor
screen, and my 'retina' resolution tends to be lower than the young
pups with their Mac Airs and Graces.  Now, I'm a pro, so I'll fit into
whatever style is agreed on, so I liked the idea of customizing the
view myself.

Lua has no such problematic choices, which is pleasing.

12