Lua
程序的源代码可以从官网下载,直接make
就好。如果遇到“fatal error: readline/readline.h: No such file or directory
”的编译错误,请参考这篇文章。
如果想自己使用gdb
研究代码,可以把src/Makefile
中的CFLAGS
一行修改如下:
CFLAGS= -g -Wall -Wextra -DLUA_COMPAT_5_2 $(SYSCFLAGS) $(MYCFLAGS)
源码编译完完会生成两个可执行文件:lua
和luac
。lua
就是语言的解释器,而luac
则是编译器,它会把lua
程序转换成字节码。
当在命令行执行lua
命令时:
[root@Fedora lua-5.3.1]# lua
Lua 5.3.1 Copyright (C) 1994-2015 Lua.org, PUC-Rio
>
在另一个窗口使用pstack
命令查看lua
进程:
[root@Fedora lua-5.3.1]# pstack 3368
#0 0x00007f8039c2a2d0 in __read_nocancel () from /lib64/libc.so.6
#1 0x00007f8039f2489d in rl_getc () from /lib64/libreadline.so.6
#2 0x00007f8039f25197 in rl_read_key () from /lib64/libreadline.so.6
#3 0x00007f8039f0ec6c in readline_internal_char () from /lib64/libreadline.so.6
#4 0x00007f8039f0f3e5 in readline () from /lib64/libreadline.so.6
#5 0x000000000040460f in pushline (L=0x233e018, firstline=1) at lua.c:310
#6 0x00000000004048b3 in loadline (L=0x233e018) at lua.c:377
#7 0x0000000000404a54 in doREPL (L=0x233e018) at lua.c:411
#8 0x00000000004051a6 in pmain (L=0x233e018) at lua.c:588
#9 0x000000000040a848 in luaD_precall (L=0x233e018, func=0x233e700, nresults=1) at ldo.c:337
#10 0x000000000040acec in luaD_call (L=0x233e018, func=0x233e700, nResults=1, allowyield=0) at ldo.c:421
#11 0x00000000004075cb in f_call (L=0x233e018, ud=0x7ffc9fae64e0) at lapi.c:919
#12 0x0000000000409ec5 in luaD_rawrunprotected (L=0x233e018, f=0x407594 <f_call>, ud=0x7ffc9fae64e0) at ldo.c:142
#13 0x000000000040b4a7 in luaD_pcall (L=0x233e018, func=0x407594 <f_call>, u=0x7ffc9fae64e0, old_top=16, ef=0) at ldo.c:644
#14 0x000000000040769a in lua_pcallk (L=0x233e018, nargs=2, nresults=1, errfunc=0, ctx=0, k=0x0) at lapi.c:945
#15 0x0000000000405273 in main (argc=1, argv=0x7ffc9fae6628) at lua.c:607
可以看到lua
程序阻塞在doREPL
函数,等待用户输入。