1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
|
# This gdb script provides several useful routines for debugging ngx_lua or
# standalone Lua/LuaJIT.
#
# You need gdb >= v7.3 to make this script working correctly.
#
# Installation: place it at $HOME/.gdbinit
#
# -- chaoslawful <at> gmail <dot> com
#### Lua type defines ####
set $__LUA_TNONE = -1
set $__LUA_TNIL = 0
set $__LUA_TBOOLEAN = 1
set $__LUA_TLIGHTUSERDATA = 2
set $__LUA_TNUMBER = 3
set $__LUA_TSTRING = 4
set $__LUA_TTABLE = 5
set $__LUA_TFUNCTION = 6
set $__LUA_TUSERDATA = 7
set $__LUA_TTHREAD = 8
#### Lua constants ####
set $__LUA_GLOBALSINDEX = -10002
set $__LUA_ENVIRONINDEX = -10001
set $__LUA_REGISTRYINDEX = -10000
#### Auxiliary methods ####
define __lua_debug_instance
if !$__lua_debug_instance
set $__lua_debug_instance = (lua_Debug*)malloc(sizeof(lua_Debug))
end
end
define __free_lua_debug_instance
if $__lua_debug_instance
set $rc = free($__lua_debug_instance)
set $__lua_debug_instance = 0
end
end
set $__BUCKET_SIZE = 16
define __set_instance
if !$__set_instance
set $__set_instance = (void*(*(*))[2])malloc($__BUCKET_SIZE*sizeof(void*(*)[2]))
set $rc = memset($__set_instance, 0, $__BUCKET_SIZE*sizeof(void*(*)[2]))
end
end
define __free_set_instance
if $__set_instance
__set_clean
set $rc = free($__set_instance)
set $__set_instance = 0
end
end
define __set_add
set $p = (void*)$arg0
set $__bkt_idx = (int)$p%$__BUCKET_SIZE
__set_instance
set $__elem = (void*(*)[2])$__set_instance[$__bkt_idx]
set $__found = 0
while $__elem
if (*$__elem)[0] == $p
set $__found = 1
loop_break
end
set $__elem = (void*(*)[2])(*$__elem)[1]
end
if $__found
set $existed_in_set = 1
else
set $existed_in_set = 0
set $rc = (void*(*)[2])calloc(1, sizeof(void*)*2)
set (*$rc)[0] = $p
set (*$rc)[1] = $__set_instance[$__bkt_idx]
set $__set_instance[$__bkt_idx] = $rc
end
end
define __set_is_exist
set $p = (void*)$arg0
set $__bkt_idx = (int)$p%$__BUCKET_SIZE
__set_instance
set $__elem = (void*(*)[2])$__set_instance[$__bkt_idx]
set $__found = 0
while $__elem
if (*$__elem)[0] == $p
set $__found = 1
loop_break
end
set $__elem = (void*(*)[2])(*$__elem)[1]
end
if $__found
set $existed_in_set = 1
else
set $existed_in_set = 0
end
end
define __set_clean
__set_instance
set $__bkt_idx = 0
while $__bkt_idx < $__BUCKET_SIZE
set $__elem = (void*(*)[2])$__set_instance[$__bkt_idx]
while $__elem
set $__next = (void*(*)[2])(*$__elem)[1]
set $rc = free($__elem)
set $__elem = $__next
end
set $__set_instance[$__bkt_idx] = 0
set $__bkt_idx = $__bkt_idx+1
end
end
define hook-quit
__free_lua_debug_instance
__free_set_instance
end
define hook-detach
__free_lua_debug_instance
__free_set_instance
end
define hook-disconnect
__free_lua_debug_instance
__free_set_instance
end
define _lua_pop
set $l = (lua_State*)$arg0
set $_n = (int)$arg1
set $_rc = lua_settop($l, -$_n-1)
end
define _lua_dump_locals
set $l = (lua_State*)$arg0
set $dbg = (lua_Debug*)$arg1
set $idx = 1
set $rc = lua_getlocal($l, $dbg, $idx)
if $rc
printf "\t----[[ Locals ]]----\n"
while $rc
printf "\t%d:\t'%s' = ", $idx, $rc
__lua_dump_stack $l -1
printf "\n"
_lua_pop $l 1
set $idx = $idx + 1
set $rc = lua_getlocal($l, $dbg, $idx)
end
else
printf "\tNo locals!\n"
end
printf "\n"
end
define _lua_dump_upvalues
set $l = (lua_State*)$arg0
set $dbg = (lua_Debug*)$arg1
set $idx = 1
set $rc = lua_getinfo($l, "f", $dbg)
if $rc
set $rc = lua_getupvalue($l, -1, $idx)
if $rc
printf "\t----[[ Upvalues ]]----\n"
while $rc
printf "\t%d:\t'%s' = ", $idx, $rc
__lua_dump_stack $l -1
printf "\n"
_lua_pop $l 1
set $idx = $idx + 1
set $rc = lua_getupvalue($l, -1, $idx)
end
else
printf "\tNo upvalues!\n"
end
_lua_pop $l 1
else
printf "\tFailed to get function closure!\n"
end
printf "\n"
end
define __lua_dump_stack
__set_clean
__lua_dump_stack_aux $arg0 $arg1 0
end
define __lua_dump_stack_aux
set $l = (lua_State*)$arg0
set $nidx_$arg2 = (int)$arg1
set $cidx_$arg2 = (int)$arg2+1
# relative stack index to absolute index
if $nidx_$arg2 < 0 && $nidx_$arg2 > $__LUA_REGISTRYINDEX
set $nidx_$arg2 = $nidx_$arg2 + (int)lua_gettop($l) + 1
end
set $vt_$arg2 = (int)lua_type($l, $nidx_$arg2)
if $vt_$arg2 == $__LUA_TNONE
echo <invalid index>
end
if $vt_$arg2 == $__LUA_TNIL
echo (nil)
end
if $vt_$arg2 == $__LUA_TBOOLEAN
printf "(bool) %d", lua_toboolean($l, $nidx_$arg2)
end
if $vt_$arg2 == $__LUA_TLIGHTUSERDATA
printf "(ludata) %p", lua_touserdata($l, $nidx_$arg2)
end
if $vt_$arg2 == $__LUA_TNUMBER
printf "%g", lua_tonumber($l, $nidx_$arg2)
end
if $vt_$arg2 == $__LUA_TSTRING
set $tmplen = (size_t*)malloc(sizeof(size_t))
set $tmp = lua_pushvalue($l, $nidx_$arg2)
set $tmp = lua_tolstring($l, -1, $tmplen)
#printf "(string:%d) ", *$tmplen
eval "output/r *(const char (*)[%d])$tmp", *$tmplen
_lua_pop $l 1
set $tmp = free($tmplen)
end
if $vt_$arg2 == $__LUA_TTABLE
set $rc = lua_topointer($l, $nidx_$arg2)
#printf "(table) %p { ", $rc
printf "{ "
__set_add $rc
if $existed_in_set
printf "... "
else
set $rc = lua_pushnil($l)
set $rc = lua_next($l, $nidx_$arg2)
while $rc != 0
printf "["
__lua_dump_stack_aux $l -2 $cidx_$arg2
printf "]"
printf " = "
__lua_dump_stack_aux $l -1 $cidx_$arg2
printf ", "
_lua_pop $l 1
set $rc = lua_next($l, $nidx_$arg2)
end
end
printf "}"
end
if $vt_$arg2 == $__LUA_TFUNCTION
printf "(func) %p", lua_topointer($l, $nidx_$arg2)
end
if $vt_$arg2 == $__LUA_TUSERDATA
printf "(udata) %p", lua_topointer($l, $nidx_$arg2)
end
if $vt_$arg2 == $__LUA_TTHREAD
printf "(thread) %p", lua_topointer($l, $nidx_$arg2)
else
if $vt_$arg2 > $__LUA_TTHREAD || $vt_$arg2 < 0
echo <unknown type>
end
end
end
#### Command methods ####
define lbt
if $argc < 1
echo Please specify Lua state and/or dump flag!\n
else
set $l = (lua_State*)$arg0
if $argc > 1
set $dump_local = ($arg1&1)==1
set $dump_upvalue = ($arg1&2)==2
else
set $dump_local = 0
set $dump_upvalue = 0
end
__lua_debug_instance
set $dbg = $__lua_debug_instance
set $level = 0
set $rc = lua_getstack($l, $level, $dbg)
while $rc > 0
set $rc = lua_getinfo($l, "Sln", $dbg)
set $name = $dbg->name
if !$name
set $name = "???"
end
printf "#%d\t%s\t[%s]\tat %s:%d\n", $level, $name, $dbg->what, $dbg->source, $dbg->currentline
if $dump_local
_lua_dump_locals $l $dbg
end
if $dump_upvalue
_lua_dump_upvalues $l $dbg
end
set $level = $level+1
set $rc = lua_getstack($l, $level, $dbg)
end
end
end
document lbt
lbt <lua state> [<dump>]: Dump the backtrace of the specified Lua state. <dump> is a mask value, whose bit 1/2 controls the dump of locals/upvalues at each stack frame correspondingly. So set <dump> to 1 dumps only locals; set to 2 dumps only upvalues; and set to 3 dumps both locals and upvalues.
end
define ll
if $argc != 2
echo Please specify Lua state and stack frame number (0-based)!\n
else
set $l = (lua_State*)$arg0
set $level = (int)$arg1
__lua_debug_instance
set $dbg = $__lua_debug_instance
set $rc = lua_getstack($l, $level, $dbg)
if $rc > 0
_lua_dump_locals $l $dbg
else
echo Failed to get Lua stack frame!\n
end
end
end
document ll
ll <lua state> <frameno>: Dump all local vars in the specified Lua stack frame (0-based).
end
define lu
if $argc != 2
echo Please specify Lua state and stack frame number (0-based)!\n
else
set $l = (lua_State*)$arg0
set $level = (int)$arg1
__lua_debug_instance
set $dbg = $__lua_debug_instance
set $rc = lua_getstack($l, $level, $dbg)
if $rc > 0
_lua_dump_upvalues $l $dbg
else
echo Failed to get Lua stack frame!\n
end
end
end
document lu
lu <lua state> <frameno>: Dump all upvalues in the specified Lua stack frame (0-based).
end
define lg
if $argc != 1
echo Please specify Lua state!\n
else
set $l = (lua_State*)$arg0
__lua_dump_stack $l $__LUA_GLOBALSINDEX
printf "\n"
end
end
document lg
lg <lua state>: Dump all entries in Lua global table.
end
define lr
if $argc != 1
echo Please specify Lua state!\n
else
set $l = (lua_State*)$arg0
__lua_dump_stack $l $__LUA_REGISTRYINDEX
printf "\n"
end
end
document lr
lr <lua state>: Dump all entries in Lua registry table.
end
define ls
if $argc != 1
echo Please specify Lua state!\n
else
set $l = (lua_State*)$arg0
set $idx = lua_gettop($l)
while $idx >= 1
printf "#%d ", $idx
__lua_dump_stack $l $idx
printf "\n"
set $idx = $idx - 1
end
end
end
document ls
ls <lua state>: Dump all entries in call stack.
end
# vi:ft=gdb ts=4 sw=4
|