📄 luajit_debug.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>Debugging LuaJIT</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><meta name="Author" content="Mike Pall"><meta name="Copyright" content="Copyright (C) 2005-2008, Mike Pall"><meta name="Language" content="en"><link rel="stylesheet" type="text/css" href="bluequad.css" media="screen"><link rel="stylesheet" type="text/css" href="bluequad-print.css" media="print"></head><body><div id="site"><a href="http://luajit.org/"><span>Lua<span id="logo">JIT</span></span></a></div><div id="head"><h1>Debugging LuaJIT</h1></div><div id="nav"><ul><li><a href="index.html">Index</a></li><li><a href="luajit.html">LuaJIT</a><ul><li><a href="luajit_features.html">Features</a></li><li><a href="luajit_install.html">Installation</a></li><li><a href="luajit_run.html">Running</a></li><li><a href="luajit_api.html">API Extensions</a></li><li><a href="luajit_intro.html">Introduction</a></li><li><a href="luajit_performance.html">Performance</a></li><li><a class="current" href="luajit_debug.html">Debugging</a></li><li><a href="luajit_changes.html">Changes</a></li></ul></li><li><a href="coco.html">Coco</a><ul><li><a href="coco_portability.html">Portability</a></li><li><a href="coco_api.html">API Extensions</a></li><li><a href="coco_changes.html">Changes</a></li></ul></li><li><a href="dynasm.html">DynASM</a><ul><li><a href="dynasm_features.html">Features</a></li><li><a href="dynasm_examples.html">Examples</a></li></ul></li><li><a href="http://luajit.org/download.html">Download <span class="ext">»</span></a></li></ul></div><div id="main"><p>LuaJIT is a rather complex application. There will undoubtedlybe bugs lurking in there. You have been warned. :-)</p><p>If you came here looking for information on how to debug<em>your application</em> (and not LuaJIT itself) then pleasecheck out <a href="luajit_api.html#jit_debug"><tt>jit.debug()</tt></a>and the <a href="luajit_run.html#j_debug"><tt>-j debug</tt></a>command line option.</p><p>But if you suspect a problem with LuaJIT itself, then tryany of the following suggestions (in order).</p><h2>Is LuaJIT the Problem?</h2><p>Try to run your application in several different ways:</p><ul><li><tt>luajit app.lua</tt></li><li><tt>luajit -O1 app.lua</tt></li><li><tt>luajit -O app.lua</tt></li><li><tt>luajit -j off app.lua</tt></li><li><tt>lua app.lua</tt> (i.e. with standard Lua)</li></ul><p>If the behaviour is the <em>same</em> as with standard Lua then ...well ... that's what LuaJIT is about: doing the same things,just faster. Even bugs fly faster. :-)</p><p>So this is most likely a bug in your application then. It may be easierto debug this with plain Lua — the remainder of this pageis probably not helpful for you.</p><p>But if the behaviour is <em>different</em>, there is some likelihoodthat you caught a bug in LuaJIT. Oh dear ...</p><p>Ok, so don't just give up. Please read on and help the communityby finding the bug. Thank you!</p><h2>Get the Latest Version</h2><p>Number one on your list of things to check is the<a href="http://luajit.org/luajit_changes.html"><span class="ext">»</span> Online Change History</a>.</p><p>Please check if a newer version is available. Maybe the bugyou have encountered has been fixed already. Always download thelatest version and try it with your application before continuing.</p><h2>Reproduce the Bug</h2><p>First try to make the bug reproducible. Try to isolate the moduleand the function the bug occurs in:</p><p>Either selectively turn off compilation for some modules with<br><tt> jit.off(true, true)</tt><br>until the bug disappears ...</p><p>And/or turn the whole JIT engine off and selectively compilefunctions with<br><tt> jit.compile(func)</tt><br>until it reappears.</p><p>If you have isolated the point where it happens, it's most helpfulto reduce the affected Lua code to a short code snippet thatstill shows the problem. You may need to <tt>print()</tt> somevariables until you can pinpoint the exact spot where it happens.</p><p>If you've got a <em>reproducible</em> and <em>short</em> testyou can either send it directly to me or the mailing list(see the <a href="contact.html">Contact Information</a>)or you can try to debug this a bit further.</p><p>Well — if you are brave enough. :-)</p><h2>Look at the Generated Code</h2><p>You may want to have a look at the output of <tt>-j dumphints</tt>first. Try to change things around until you can see which hintor which instruction is the cause of the bug. If you suspectan optimizer bug then have a look at the backend (<tt>*.das[ch]</tt>)and check how the hint is encoded.</p><p>Otherwise have a look at <tt>-j dump</tt> and see whetheryou can spot the problem around the affected instruction.It's helpful to have a good knowledge of assembler, though(sorry).</p><h2>Locate a Crash</h2><p>If you get a crash, you should compile LuaJIT with debuggingturned on:</p><p>Add <tt>-g</tt> to <tt>CFLAGS</tt> and <tt>MYLDFLAGS</tt>or whatever is needed to turn on debugging. For Windows youneed both an executable and a DLL built with debugging.</p><p>Then start LuaJIT with your debugger. Run it with<tt>-j dump=test.dump</tt>.</p><p>Have a look at the backtrace and compare it with the generateddump file to find out exactly where it crashes. I'm sorry, butsymbols or instructions for JIT compiled functions are notdisplayed in your debugger (this is really hard to solve).</p><h2>Turn on Assertions</h2><p>Another way to debug LuaJIT is to turn on assertions.They can be turned on only for the JIT engine by adding<tt>-DLUAJIT_ASSERT</tt> to <tt>JITCFLAGS</tt> in <tt>src/Makefile</tt>.Then recompile with <tt>make clean</tt> and <tt>make</tt>.</p><p>Add these two lines to <tt>src/luaconf.h</tt> to turn on all assertions in the Lua core:<br><tt> #include <assert.h></tt><br><tt> #define lua_assert(x) assert(x)</tt><br>This turns on the JIT engine assertions, too.Recompile and see whether any assertions trigger.Don't forget to turn off the (slow) assertions when you're done!</p><h2>Use Valgrind</h2><p>A tremendously useful (and free) tool for runtime code analysisis <a href="http://valgrind.org/"><span class="ext">»</span> Valgrind</a>. Regularlyrun your applications with <tt>valgrind --memcheck</tt> andyour life will be better.</p><p>To run LuaJIT under Valgrind you <em>must</em> add<tt>-DUSE_VALGRIND</tt> to <tt>MYCFLAGS</tt>and recompile LuaJIT. You will get random errors if you don't!Valgrind 3.x or later is required. Earlier versionsdo not work well with newly allocated C stacks.</p><p>An executable built with this option runs fine without Valgrindand without a performance loss. But it needs the Valgrind headerfiles for compilation (which is why it's not enabled by default).</p><p>It's helpful to compile LuaJIT with debugging turned on, too(see above).</p><p>If Valgrind spots many invalid memory accesses that involvememory allocation/free functions you've probably found a bugrelated to garbage collection. Some object reference must havegone astray.</p><p>Try to find out which object is disappearing. You can forceeager garbage collection with repeated calls to<tt>collectgarbage()</tt> or by setting a very low thresholdwith <tt>collectgarbage("setpause", 1)</tt>.</p><h2>Don't Despair</h2><p>If all of this doesn't help to find the bug, please senda summary of your findings to the mailing list. Describe as muchof the circumstances you think are relevant.</p><p>Please <em>don't</em> send your whole application to me(without asking first) and especially not to the mailing list.Code snippets should preferrably be less than 50 lines andup to the point.</p><p>All bug reports are helpful, even if no immediate solutionis available. Often enough someone else finds the same bugin a different setting and together with your bug reportthis may help to track it down.</p><p>Finally I have to say a <strong>BIG THANK YOU</strong>to everyone who has helped to make LuaJIT better by findingand fixing bugs!</p><br class="flush"></div><div id="foot"><hr class="hide">Copyright © 2005-2008 Mike Pall<span class="noprint">·<a href="contact.html">Contact</a></span></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -