diff --git a/README.md b/README.md index cb1cb1a..ee435e4 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,3 @@ -Lua GDB Helpers -=============== - -This is a collection of several GDB macros that simplify debugging of C modules for Lua. Lua has to be compiled with debugging symbols, or the appropriate "dbg" package (i.e. `liblua5.1-0-dbg` in Debian/Ubuntu) needs to be installed. - Installation & Running ---------------------- @@ -12,21 +7,4 @@ To include the macros into your GDB session, run the following command: source -It is advisable to include the line into your `~/.gdbinit` file (see the [GDB documentation](http://sourceware.org/gdb/current/onlinedocs/gdb/Startup.html)), so you do not have to type the command everytime you start GDB. - -Reference ---------- - -- luastack [L] - lists the values on the current Lua C stack. By default, uses the current variable `L` as the `lua_State` pointer, however, an alternate `lua_State` can be provided as the first argument if needed. - -- luaprint < value > [verbose] - Pretty-prints a `TValue` passed as argument. Expects a pointer to a `TValue`. When verbose is 1, expands tables, metatables and userdata environments. - -- luaprinttable < table > - Pretty-prints a Lua Table. Expects a pointer to `Table`. - -- luavalue < index > [L] - Provides a pointer to a TValue from a stack index. By default, uses the current variable L as a lua_State pointer, but can be specified as the second argument. The return value is passed in variable $obj. - -However, note that "pretty-printing" is not as pretty as it could be. First, the hash part is displayed, then the array part. Entries in hash part are separated by newlines, the ones in array part are displayed side-by-side. - -License -------- -Copyright (c) 2010 Michal Kottman, MIT License +Use now `luatrace [L-state]` to print a traceback of the Lua stack (default variable `L`). diff --git a/luagdb.txt b/luagdb.txt index 958be64..e711a35 100644 --- a/luagdb.txt +++ b/luagdb.txt @@ -1,182 +1,22 @@ -# Lua GDB Helpers -# Copyright (c) 2010 Michal Kottman -# License: MIT - -define luavalue - if $argc == 0 - help luavalue - else - if $argc == 2 - set $L = $arg1 - else - set $L = L - end - set $obj = index2adr($L, $arg0) - end -end -document luavalue - luavalue [L] - Provides a pointer to a TValue from a stack index. By default, uses the current variable - L as a lua_State pointer, but can be specified as the second argument. The return value - is passed in variable $obj. -end - -define luaprinttable - if $argc != 1 - help luaprinttable - else - printf " { " - set $t = $arg0 - set $node = $t->node - set $i = 0 - set $last = 1 << $t->lsizenode - # hash part - while $i < $last - set $node = $t->node + $i - set $key = $node->i_key - if $key.tvk.tt > 0 - if $key.tvk.tt == 4 - # string key - set $ts = &($key.tvk.value->gc->ts) - set $str = (char *)($ts + 1) - printf "%s = ", $str - else - printf "<%s> = ", lua_typename(L, $key.tvk.tt) - end - set $val = &($node->i_val) - luaprint $val - printf ",\n" - end - set $i = $i + 1 - end - # array part - set $i = 0 - while $i < $t->sizearray - set $val = $t->array + $i - luaprint $val - printf ", " - set $i = $i + 1 - end - printf " } " - end -end -document luaprinttable - luaprinttable - Pretty-prints a Lua Table. Expects a pointer to Table. -end - -define luaprint - if $argc == 0 - help luaprint - else - set $v = $arg0 - set $val = &($v.value) - set $type = $v.tt - - if $type == 0 - printf "nil" - end - # boolean - if $type == 1 - if $val->b > 0 - printf "true" - else - printf "false" - end - end - # lightudata - if $type == 2 - printf "%p", $val->p - end - # number - if $type == 3 - printf "%f", $val->n - end - # string - if $type == 4 - set $ts = &($val->gc->ts) - set $str = (char *)($ts + 1) - printf "'%s'", $str - end - # table - if $type == 5 - set $tab = &($val->gc->h) - printf " %p ", $tab - if $argc == 2 - luaprinttable $tab - if $tab.metatable - printf " metatable=" - luaprinttable $tab.metatable - end - end - end - # userdata - if $type == 7 - set $uv = $val->gc.u->uv - printf " %p size=%d", &($val->gc.u) + 1, $uv.len - if $argc == 2 && $uv.metatable - printf " metatable=" - luaprinttable $uv.metatable - end - if $argc == 2 && $uv.env - printf " env=" - luaprinttable $uv.env - end - end - # other - if $type > 5 && $type != 7 - printf "<%s> %p", lua_typename(L, $type), $val - end - end -end -document luaprint - luaprint [verbose] - Pretty-prints a TValue passed as argument. Expects a pointer to a TValue. When verbose is 1, - expands tables, metatables and userdata environments. -end - -define luastack - if $argc == 0 - set $L = L - else - set $L = $arg0 - end - printf "Lua stack trace: %d items\n", lua_gettop($L) - set $ptr = $L->base - set $idx = 1 - while $ptr < $L->top - printf "%03d: ", $idx - luaprint $ptr - printf "\n" - set $ptr = $ptr + 1 - set $idx = $idx + 1 - end -end -document luastack - luastack [L] - Prints values on the Lua C stack. Without arguments, uses the current value of "L" - as the lua_State*. You can provide an alternate lua_State as the first argument. -end - define luatrace - if $argc == 0 - set $L = L - else - set $L = $arg0 - end - if luaL_loadstring (L, "return debug.traceback()") == 0 - if lua_pcall(L, 0, 1, 0) == 0 - printf "%s\n", lua_tolstring (L, -1, 0) - else - printf "ERROR: %s\n", lua_tolstring (L, -1, 0) - end - call lua_settop (L, -2) - end + if $argc == 0 + set $L = L + else + set $L = $arg0 + end + if luaL_loadstring ($L, "return require'debug'.traceback(nil, 2)") == 0 + if lua_pcallk($L, 0, 1, 0, 0, 0) == 0 + printf "%s\n", lua_tolstring ($L, -1, 0) + else + printf "ERROR: %s\n", lua_tolstring ($L, -1, 0) + end + call lua_settop ($L, -2) + end end document luatrace - luatraceback [L] - Dumps Lua execution stack, as debug.traceback() does. Without - arguments, uses the current value of "L" as the - lua_State*. You can provide an alternate lua_State as the - first argument. + luatrace [L] + Dumps Lua execution stack, as debug.traceback() does. Without + arguments, uses the current value of "L" as the + lua_State*. You can provide an alternate lua_State as the + first argument. end