Tracing execution of Lua Sripts












1














I know you can use the debug library of lua to get some tracing info/debugging. But the information is in pieces. So I am wondering if there is a way to trace the execution of a Lua script. A step by step process. it would be required and it will automatically go thru at every execution step To produce a report such as the following;



Called: function xyz  from : Table abc
It has n parameters
Param 1: apples
Param 2: oranges
.
.

It has m returns
return 1: red
return 2: yellow
.
.

Called: function xyz2 from : Table abc2
It has n parameters
Param 1: pears
Param 2: bananas
.
.
It has m reruns
return 1: heavy
return 2: light
.
.

and so on....









share|improve this question





























    1














    I know you can use the debug library of lua to get some tracing info/debugging. But the information is in pieces. So I am wondering if there is a way to trace the execution of a Lua script. A step by step process. it would be required and it will automatically go thru at every execution step To produce a report such as the following;



    Called: function xyz  from : Table abc
    It has n parameters
    Param 1: apples
    Param 2: oranges
    .
    .

    It has m returns
    return 1: red
    return 2: yellow
    .
    .

    Called: function xyz2 from : Table abc2
    It has n parameters
    Param 1: pears
    Param 2: bananas
    .
    .
    It has m reruns
    return 1: heavy
    return 2: light
    .
    .

    and so on....









    share|improve this question



























      1












      1








      1


      0





      I know you can use the debug library of lua to get some tracing info/debugging. But the information is in pieces. So I am wondering if there is a way to trace the execution of a Lua script. A step by step process. it would be required and it will automatically go thru at every execution step To produce a report such as the following;



      Called: function xyz  from : Table abc
      It has n parameters
      Param 1: apples
      Param 2: oranges
      .
      .

      It has m returns
      return 1: red
      return 2: yellow
      .
      .

      Called: function xyz2 from : Table abc2
      It has n parameters
      Param 1: pears
      Param 2: bananas
      .
      .
      It has m reruns
      return 1: heavy
      return 2: light
      .
      .

      and so on....









      share|improve this question















      I know you can use the debug library of lua to get some tracing info/debugging. But the information is in pieces. So I am wondering if there is a way to trace the execution of a Lua script. A step by step process. it would be required and it will automatically go thru at every execution step To produce a report such as the following;



      Called: function xyz  from : Table abc
      It has n parameters
      Param 1: apples
      Param 2: oranges
      .
      .

      It has m returns
      return 1: red
      return 2: yellow
      .
      .

      Called: function xyz2 from : Table abc2
      It has n parameters
      Param 1: pears
      Param 2: bananas
      .
      .
      It has m reruns
      return 1: heavy
      return 2: light
      .
      .

      and so on....






      debugging lua trace






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 18:18

























      asked Nov 20 at 18:13









      Dave Kay

      505




      505
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Here is some code that used to be distributed in the Lua tarball. It's from 2005 and still works fine.



          -- trace calls
          -- example: lua -ltrace-calls bisect.lua

          local level=0

          local function hook(event)
          local t=debug.getinfo(3)
          io.write(level," >>> ",string.rep(" ",level))
          if t~=nil and t.currentline>=0 then io.write(t.short_src,":",t.currentline," ") end
          t=debug.getinfo(2)
          if event=="call" then
          level=level+1
          else
          level=level-1 if level<0 then level=0 end
          end
          if t.what=="main" then
          if event=="call" then
          io.write("begin ",t.short_src)
          else
          io.write("end ",t.short_src)
          end
          elseif t.what=="Lua" then
          io.write(event," ",t.name or "(Lua)"," <",t.linedefined,":",t.short_src,">")
          else
          io.write(event," ",t.name or "(C)"," [",t.what,"] ")
          end
          io.write("n")
          end

          debug.sethook(hook,"cr")
          level=0





          share|improve this answer





















          • This is great, thank you. Any idea how to capture the function arguments, return values and which table we are in at the moment?
            – Dave Kay
            Nov 20 at 23:31












          • why is it that if I add anything even a simple print statement, the whole thing falls apart. I placed print(t.name) after the line t=debug.getinfo(2) and the output fell apart. I am using zerobrane. Lua 5.1. I tried replacing io.write with print everywhere and that messed up everything as well. I have zero experience with io.write
            – Dave Kay
            Nov 21 at 7:25










          • @DaveKay, because print adds a newline and io.write does not.
            – lhf
            Nov 21 at 10:13










          • @Ihf, Then how does one debug lua? embedded in C/C++ engine. Where one has no access to the engine and can not do remote debugging for variety of reasons. I mean of'course other than putting print statements everywhere (cause that's what I am literally doing now). There's got to be some trace script somewhere that can trace execution of lua scripts and produce a report
            – Dave Kay
            Nov 25 at 2:38













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399079%2ftracing-execution-of-lua-sripts%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Here is some code that used to be distributed in the Lua tarball. It's from 2005 and still works fine.



          -- trace calls
          -- example: lua -ltrace-calls bisect.lua

          local level=0

          local function hook(event)
          local t=debug.getinfo(3)
          io.write(level," >>> ",string.rep(" ",level))
          if t~=nil and t.currentline>=0 then io.write(t.short_src,":",t.currentline," ") end
          t=debug.getinfo(2)
          if event=="call" then
          level=level+1
          else
          level=level-1 if level<0 then level=0 end
          end
          if t.what=="main" then
          if event=="call" then
          io.write("begin ",t.short_src)
          else
          io.write("end ",t.short_src)
          end
          elseif t.what=="Lua" then
          io.write(event," ",t.name or "(Lua)"," <",t.linedefined,":",t.short_src,">")
          else
          io.write(event," ",t.name or "(C)"," [",t.what,"] ")
          end
          io.write("n")
          end

          debug.sethook(hook,"cr")
          level=0





          share|improve this answer





















          • This is great, thank you. Any idea how to capture the function arguments, return values and which table we are in at the moment?
            – Dave Kay
            Nov 20 at 23:31












          • why is it that if I add anything even a simple print statement, the whole thing falls apart. I placed print(t.name) after the line t=debug.getinfo(2) and the output fell apart. I am using zerobrane. Lua 5.1. I tried replacing io.write with print everywhere and that messed up everything as well. I have zero experience with io.write
            – Dave Kay
            Nov 21 at 7:25










          • @DaveKay, because print adds a newline and io.write does not.
            – lhf
            Nov 21 at 10:13










          • @Ihf, Then how does one debug lua? embedded in C/C++ engine. Where one has no access to the engine and can not do remote debugging for variety of reasons. I mean of'course other than putting print statements everywhere (cause that's what I am literally doing now). There's got to be some trace script somewhere that can trace execution of lua scripts and produce a report
            – Dave Kay
            Nov 25 at 2:38


















          2














          Here is some code that used to be distributed in the Lua tarball. It's from 2005 and still works fine.



          -- trace calls
          -- example: lua -ltrace-calls bisect.lua

          local level=0

          local function hook(event)
          local t=debug.getinfo(3)
          io.write(level," >>> ",string.rep(" ",level))
          if t~=nil and t.currentline>=0 then io.write(t.short_src,":",t.currentline," ") end
          t=debug.getinfo(2)
          if event=="call" then
          level=level+1
          else
          level=level-1 if level<0 then level=0 end
          end
          if t.what=="main" then
          if event=="call" then
          io.write("begin ",t.short_src)
          else
          io.write("end ",t.short_src)
          end
          elseif t.what=="Lua" then
          io.write(event," ",t.name or "(Lua)"," <",t.linedefined,":",t.short_src,">")
          else
          io.write(event," ",t.name or "(C)"," [",t.what,"] ")
          end
          io.write("n")
          end

          debug.sethook(hook,"cr")
          level=0





          share|improve this answer





















          • This is great, thank you. Any idea how to capture the function arguments, return values and which table we are in at the moment?
            – Dave Kay
            Nov 20 at 23:31












          • why is it that if I add anything even a simple print statement, the whole thing falls apart. I placed print(t.name) after the line t=debug.getinfo(2) and the output fell apart. I am using zerobrane. Lua 5.1. I tried replacing io.write with print everywhere and that messed up everything as well. I have zero experience with io.write
            – Dave Kay
            Nov 21 at 7:25










          • @DaveKay, because print adds a newline and io.write does not.
            – lhf
            Nov 21 at 10:13










          • @Ihf, Then how does one debug lua? embedded in C/C++ engine. Where one has no access to the engine and can not do remote debugging for variety of reasons. I mean of'course other than putting print statements everywhere (cause that's what I am literally doing now). There's got to be some trace script somewhere that can trace execution of lua scripts and produce a report
            – Dave Kay
            Nov 25 at 2:38
















          2












          2








          2






          Here is some code that used to be distributed in the Lua tarball. It's from 2005 and still works fine.



          -- trace calls
          -- example: lua -ltrace-calls bisect.lua

          local level=0

          local function hook(event)
          local t=debug.getinfo(3)
          io.write(level," >>> ",string.rep(" ",level))
          if t~=nil and t.currentline>=0 then io.write(t.short_src,":",t.currentline," ") end
          t=debug.getinfo(2)
          if event=="call" then
          level=level+1
          else
          level=level-1 if level<0 then level=0 end
          end
          if t.what=="main" then
          if event=="call" then
          io.write("begin ",t.short_src)
          else
          io.write("end ",t.short_src)
          end
          elseif t.what=="Lua" then
          io.write(event," ",t.name or "(Lua)"," <",t.linedefined,":",t.short_src,">")
          else
          io.write(event," ",t.name or "(C)"," [",t.what,"] ")
          end
          io.write("n")
          end

          debug.sethook(hook,"cr")
          level=0





          share|improve this answer












          Here is some code that used to be distributed in the Lua tarball. It's from 2005 and still works fine.



          -- trace calls
          -- example: lua -ltrace-calls bisect.lua

          local level=0

          local function hook(event)
          local t=debug.getinfo(3)
          io.write(level," >>> ",string.rep(" ",level))
          if t~=nil and t.currentline>=0 then io.write(t.short_src,":",t.currentline," ") end
          t=debug.getinfo(2)
          if event=="call" then
          level=level+1
          else
          level=level-1 if level<0 then level=0 end
          end
          if t.what=="main" then
          if event=="call" then
          io.write("begin ",t.short_src)
          else
          io.write("end ",t.short_src)
          end
          elseif t.what=="Lua" then
          io.write(event," ",t.name or "(Lua)"," <",t.linedefined,":",t.short_src,">")
          else
          io.write(event," ",t.name or "(C)"," [",t.what,"] ")
          end
          io.write("n")
          end

          debug.sethook(hook,"cr")
          level=0






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 18:47









          lhf

          55.6k667103




          55.6k667103












          • This is great, thank you. Any idea how to capture the function arguments, return values and which table we are in at the moment?
            – Dave Kay
            Nov 20 at 23:31












          • why is it that if I add anything even a simple print statement, the whole thing falls apart. I placed print(t.name) after the line t=debug.getinfo(2) and the output fell apart. I am using zerobrane. Lua 5.1. I tried replacing io.write with print everywhere and that messed up everything as well. I have zero experience with io.write
            – Dave Kay
            Nov 21 at 7:25










          • @DaveKay, because print adds a newline and io.write does not.
            – lhf
            Nov 21 at 10:13










          • @Ihf, Then how does one debug lua? embedded in C/C++ engine. Where one has no access to the engine and can not do remote debugging for variety of reasons. I mean of'course other than putting print statements everywhere (cause that's what I am literally doing now). There's got to be some trace script somewhere that can trace execution of lua scripts and produce a report
            – Dave Kay
            Nov 25 at 2:38




















          • This is great, thank you. Any idea how to capture the function arguments, return values and which table we are in at the moment?
            – Dave Kay
            Nov 20 at 23:31












          • why is it that if I add anything even a simple print statement, the whole thing falls apart. I placed print(t.name) after the line t=debug.getinfo(2) and the output fell apart. I am using zerobrane. Lua 5.1. I tried replacing io.write with print everywhere and that messed up everything as well. I have zero experience with io.write
            – Dave Kay
            Nov 21 at 7:25










          • @DaveKay, because print adds a newline and io.write does not.
            – lhf
            Nov 21 at 10:13










          • @Ihf, Then how does one debug lua? embedded in C/C++ engine. Where one has no access to the engine and can not do remote debugging for variety of reasons. I mean of'course other than putting print statements everywhere (cause that's what I am literally doing now). There's got to be some trace script somewhere that can trace execution of lua scripts and produce a report
            – Dave Kay
            Nov 25 at 2:38


















          This is great, thank you. Any idea how to capture the function arguments, return values and which table we are in at the moment?
          – Dave Kay
          Nov 20 at 23:31






          This is great, thank you. Any idea how to capture the function arguments, return values and which table we are in at the moment?
          – Dave Kay
          Nov 20 at 23:31














          why is it that if I add anything even a simple print statement, the whole thing falls apart. I placed print(t.name) after the line t=debug.getinfo(2) and the output fell apart. I am using zerobrane. Lua 5.1. I tried replacing io.write with print everywhere and that messed up everything as well. I have zero experience with io.write
          – Dave Kay
          Nov 21 at 7:25




          why is it that if I add anything even a simple print statement, the whole thing falls apart. I placed print(t.name) after the line t=debug.getinfo(2) and the output fell apart. I am using zerobrane. Lua 5.1. I tried replacing io.write with print everywhere and that messed up everything as well. I have zero experience with io.write
          – Dave Kay
          Nov 21 at 7:25












          @DaveKay, because print adds a newline and io.write does not.
          – lhf
          Nov 21 at 10:13




          @DaveKay, because print adds a newline and io.write does not.
          – lhf
          Nov 21 at 10:13












          @Ihf, Then how does one debug lua? embedded in C/C++ engine. Where one has no access to the engine and can not do remote debugging for variety of reasons. I mean of'course other than putting print statements everywhere (cause that's what I am literally doing now). There's got to be some trace script somewhere that can trace execution of lua scripts and produce a report
          – Dave Kay
          Nov 25 at 2:38






          @Ihf, Then how does one debug lua? embedded in C/C++ engine. Where one has no access to the engine and can not do remote debugging for variety of reasons. I mean of'course other than putting print statements everywhere (cause that's what I am literally doing now). There's got to be some trace script somewhere that can trace execution of lua scripts and produce a report
          – Dave Kay
          Nov 25 at 2:38




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399079%2ftracing-execution-of-lua-sripts%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen