Skip to content

Instantly share code, notes, and snippets.

@jwise
Created October 16, 2011 03:40
Show Gist options
  • Save jwise/1290477 to your computer and use it in GitHub Desktop.
Save jwise/1290477 to your computer and use it in GitHub Desktop.
A few short scripts to walk through Lua tables in GDB
(gdb) lua_globals L
(gdb) lua_gettype $rv
(gdb) lua_gettable $rv "string"
Walking node part...
Value at node idx 1 Key [STRING]: string -> value type 5 @ (TValue *)0x81012cc
(gdb) lua_gettype $rv
(gdb) lua_walktable $rv
Walking array part...
Walking node part...
Value at node idx 0 Key [STRING]: sub -> value type 6 @ (TValue*)0x8100160
Value at node idx 2 Key [STRING]: upper -> value type 6 @ (TValue*)0x8100198
Value at node idx 3 Key [STRING]: len -> value type 6 @ (TValue*)0x81001b4
Value at node idx 4 Key [STRING]: gfind -> value type 6 @ (TValue*)0x81001d0
Value at node idx 5 Key [STRING]: rep -> value type 6 @ (TValue*)0x81001ec
Value at node idx 6 Key [STRING]: find -> value type 6 @ (TValue*)0x8100208
Value at node idx 7 Key [STRING]: match -> value type 6 @ (TValue*)0x8100224
Value at node idx 8 Key [STRING]: char -> value type 6 @ (TValue*)0x8100240
Value at node idx 9 Key [STRING]: dump -> value type 6 @ (TValue*)0x810025c
Value at node idx 10 Key [STRING]: gmatch -> value type 6 @ (TValue*)0x8100278
Value at node idx 11 Key [STRING]: reverse -> value type 6 @ (TValue*)0x8100294
Value at node idx 12 Key [STRING]: byte -> value type 6 @ (TValue*)0x81002b0
Value at node idx 13 Key [STRING]: format -> value type 6 @ (TValue*)0x81002cc
Value at node idx 14 Key [STRING]: gsub -> value type 6 @ (TValue*)0x81002e8
Value at node idx 15 Key [STRING]: lower -> value type 6 @ (TValue*)0x8100304
It's pretty arcane, but it *does* work, and it is *much* better than nothing.
define lua_globals
set $rv = $arg0.l_gt
end
define lua_gettype
set $internal = $arg0
if $internal.tt == 0
set $type = "NIL"
set $rv = NULL
end
if $internal.tt == 1
set $type = "BOOLEAN"
print "I don't even know how to LUA_TBOOLEAN!"
end
if $internal.tt == 2
set $type = "LIGHTUSERDATA"
set $rv = $internal.value.p
end
if $internal.tt == 3
set $type = "NUMBER"
set $rv = $internal.value.n
end
if $internal.tt == 4
set $type = "STRING"
set $rv = (char *)((TString*)$internal.value.gc+1)
end
if $internal.tt == 5
set $type = "TABLE"
set $rv = *(Table*)$internal.value.gc
end
if $internal.tt == 6
set $type = "FUNCTION"
print "I don't even know how to LUA_TFUNCTION!"
end
if $internal.tt == 7
set $type = "USERDATA"
print "I don't even know how to LUA_TUSERDATA!"
end
if $internal.tt == 8
set $type = "THREAD"
print "I don't even know how to LUA_TTHREAD!"
end
end
define lua_walktable
set $int0 = $arg0
set $max = $int0.sizearray
set $cur = 0
printf "Walking array part...\n"
while $cur != $max
set $curelt = $int0.array[$cur]
if $curelt.tt != 0
printf "Value at array idx %d has type %d\n", $cur, $curelt.tt
end
set $cur = $cur + 1
end
set $cur = 0
set $max = 1 << $int0.lsizenode
printf "Walking node part...\n"
while $cur != $max
set $curelt = $int0.node[$cur]
if $curelt.i_val.tt != 0 && $curelt.i_key.tvk.tt != 0
set $ikey = $curelt.i_key.tvk
set $ival = $curelt.i_val
lua_gettype $ikey
printf "Value at node idx %d\tKey [%s]: %15s \t -> value type %d @ (TValue*)%p\n", $cur, $type, $rv, $curelt.i_val.tt, &$int0.node[$cur].i_val
end
set $cur = $cur + 1
end
end
define lua_gettable
set $int0 = $arg0
set $keystr = $arg1
set $cur = 0
set $max = 1 << $int0.lsizenode
printf "Walking node part...\n"
while $cur != $max
set $curelt = $int0.node[$cur]
if $curelt.i_val.tt != 0 && $curelt.i_key.tvk.tt != 0
set $ikey = $curelt.i_key.tvk
set $ival = $curelt.i_val
lua_gettype $ikey
set $kv = $rv
strcmp $kv $keystr
if $rv == 1
printf "Value at node idx %d\tKey [%s]: %15s \t -> value type %d @ (TValue *)%p\n", $cur, $type, $kv, $curelt.i_val.tt, &$int0.node[$cur].i_val
set $realrv = &$int0.node[$cur].i_val
end
end
set $cur = $cur + 1
end
set $rv = $realrv
end
define strcmp
set $v0 = (char *)$arg0
set $v1 = (char *)$arg1
while ((*$v0 != 0) && (*$v1 != 0) && (*$v0 == *$v1))
set $v0 = (char *)($v0 + 1)
set $v1 = (char *)($v1 + 1)
end
set $rv = *$v0 == *$v1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment