supplierdeeply

debugging chdk code - how do you do it?

  • 32 Replies
  • 4200 Views
Re: debugging chdk code - how do you do it?
« Reply #20 on: 07 / September / 2009, 18:23:26 »
Advertisements
I think I have it wrapped up. comments welcome. oh, am i supposed to deliver this somewhere else?

I'll see if I can learn something about debugging lua this week.

Re: debugging chdk code - how do you do it?
« Reply #21 on: 10 / September / 2009, 07:24:24 »
I've tried to make heads or tails of the lua debugging features and have failed thus far.

lua_pcall doesn't return a value until the stack is unwound which is too late for debugging.

lua_getstack and lua_getinfo seem to be the functions to use but i'm uncertain how i should apply them.

*

Offline reyalp

  • ******
  • 10069
Re: debugging chdk code - how do you do it?
« Reply #22 on: 10 / September / 2009, 14:38:45 »
lua_pcall doesn't return a value until the stack is unwound which is too late for debugging.
lua_resume is where your error would be happening (in kbd.c process_script). pcall is only used for the "restore" function. In any case, one of the three error values mentioned above should be returned. Knowing which one it was might be an important clue.
Don't forget what the H stands for.

Re: debugging chdk code - how do you do it?
« Reply #23 on: 13 / September / 2009, 14:27:46 »
here's my results thus far and the code to produce it

i'm reproduced the bug twice.
Lres - 820046
what would normally be the error text - 288
--
2nd run -
820042
367
(this saved 82004 to the text file)

Code: [Select]
if (Lres != LUA_YIELD && Lres != 0) {
     char buffer[20];
     sprintf(buffer,"%i",Lres);
 
     script_console_add_line(buffer); 
     script_console_add_line( lua_tostring( Lt, -1 ) );

     FILE *pFile = fopen ("A/myfile.txt","a");
     fwrite (buffer,1,sizeof(buffer),pFile);
     fclose (pFile);

it should be noted that when i run the following code it errors properly, Lres returning 2 and saving it to the text file.

This is the code for my demo bug -

Code: [Select]
function failFn()
print(math.abs("NaN"))
end
failFn();


*

Offline reyalp

  • ******
  • 10069
Re: debugging chdk code - how do you do it?
« Reply #24 on: 13 / September / 2009, 18:34:50 »
Given that the error value isn't one of the expected error values, I'd guess that one of the places where the lua code expects to panic and exit is getting hit. Since exit isn't actually called, you'd get garbage instead. It should be fairly easy to go through the lua code and record if any of these are hit.
Don't forget what the H stands for.

Re: debugging chdk code - how do you do it?
« Reply #25 on: 13 / September / 2009, 18:41:03 »
there's 1.7 MB of lua code, at least if you're talking about the lua lib.

even if its easy, i could use a pointer on where to look.

*

Offline reyalp

  • ******
  • 10069
Re: debugging chdk code - how do you do it?
« Reply #26 on: 13 / September / 2009, 19:49:23 »
there's 1.7 MB of lua code, at least if you're talking about the lua lib.
Not source ;)
Quote
even if its easy, i could use a pointer on where to look.
if you grep for exit( you will find it is used in exactly one relevant place: ldo.c luaD_throw

That in turn is called a handful of places.

As I suggested earlier, if you install a panic handler function (which is described in TFM), this will be called in every case that exit() would be called.

Looking a bit more, I'm not confident this is the problem, but it'd be another data point.

You could also throw some debugging code into ldo.c lua_resume to see which code path you are getting that weird status value from.
Don't forget what the H stands for.

Re: debugging chdk code - how do you do it?
« Reply #27 on: 13 / September / 2009, 23:23:07 »
i'm trying to add my text writing code where ever luaD_throw is called. however, it doesn't work in lmem.c. i tried adding #include <stdio.h> to lmem.h but i get the same result. ideas?

this is the compile error -

Code: [Select]
lmem.c: In function 'luaM_realloc_':
lmem.c:81: error: 'FILE' undeclared (first use in this function)
lmem.c:81: error: (Each undeclared identifier is reported only once
lmem.c:81: error: for each function it appears in.)
lmem.c:81: error: 'pFile' undeclared (first use in this function)
lmem.c:81: warning: implicit declaration of function 'fopen'
lmem.c:82: warning: implicit declaration of function 'fwrite'
lmem.c:82: warning: incompatible implicit declaration of built-in function 'fwrite'
lmem.c:83: warning: implicit declaration of function 'fclose'
C:\gcc4\bin\gmake[2]: *** [lmem.o] Error 1
C:\gcc4\bin\gmake[1]: *** [all-recursive] Error 1
gmake: *** [all-recursive] Error 1

i've also added debugging info to all the possible paths through lua_resume.

my example fail code above produces this -

lua_resume status != 0


Let me know if theres anything else i should be looking for.


*

Offline reyalp

  • ******
  • 10069
Re: debugging chdk code - how do you do it?
« Reply #28 on: 13 / September / 2009, 23:43:09 »
all of the libc stuff in CHDK is in stdlib.h
Don't forget what the H stands for.

Re: debugging chdk code - how do you do it?
« Reply #29 on: 15 / September / 2009, 07:01:45 »
this is where it looks like the chdk code is exiting, from lua_resume -

Code: [Select]
 if (status != 0) {  /* error? */
       FILE *pFile = fopen ("A/myfile.txt","a");
fwrite ("lua_resume status != 0\n",1,sizeof("lua_resume status != 0\n"),pFile);
fclose (pFile);
  
    L->status = cast_byte(status);  /* mark thread as `dead' */
    luaD_seterrorobj(L, status, L->top);
    L->ci->top = L->top;
  }

which produced the following output to my log -

Code: [Select]
lua_resume status != 0
exit error info
820166

 

Related Topics