Re: Debug hooks and JIT

  • From: Geoff Leyland <geoff_leyland@xxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 11 Oct 2012 18:59:40 +1300

On 11/10/2012, at 12:20 PM, Scott Lembcke <slembcke@xxxxxxxxx> wrote:
> So just saw referenced in an old post that Mike said:
> 
>> Trace recording is aborted if a hook is called. If you don't call
>> them all the time, then something will eventually be compiled of
>> course. Since compiled code doesn't call any hooks, it won't show
>> up in your profile.
>> 
>> In other words: use jit.off() or the C equivalent if you want to
>> profile Lua code in the LuaJIT interpreter.
> 
> Is this still true? I've noticed some weirdness occasionally with the CLI 
> debugger I made. Do I need to explicitly disable the JIT when setting a hook 
> and then reset it when I remove the hook? It would mildly nice if the 
> presence of a hook automatically suppressed the JIT, but whatever. It's not 
> like it's a common API to use, and it's like a 5 line workaround (if you knew 
> to do it).

I thought that calling hooks did not prevent traces being compiled, but that 
compiled code didn't call hooks.  The above seems to imply that hooks prevent 
traces being recorded.

In any case, you can do a test:

local a = 0
debug.sethook(function() a = a + 1 end, "l")

local j = 0
for i = 1, 1000 do
  j = j + i
end

debug.sethook()
print(a, j)

$ lua test.lua
2003    500500
$ luajit test.lua
2003    500500
$ luajit -jdump test.lua
...
---- TRACE 2 start test.lua:5
0012  ADDVV    1   1   5
---- TRACE 2 abort test.lua:2 -- error thrown or hook called during recording

which seems to imply traces aren't recorded.  Odd, because luatrace doesn't 
work with luajit unless you use -joff.

Cheers,
Geoff

Other related posts: