[Tarantool-patches] [PATCH 1/2] gdb: unwind Lua stack top part

Igor Munkin imun at tarantool.org
Tue Feb 1 00:33:54 MSK 2022


Prior to this patch the dump of Lua stack top (or bottom if we consider
address growth direction) part was almost hardcoded except the number of
its free slots. This leads to the following drawbacks of <lj-stack>
command:
* Red zone stack slots are collapsed like the way similar to free stack
  slots are except for the single difference: one needs to use <lj-tv>
  to inspect them regardless the fact they are used or not.
* Top slot is hardcoded, that is confusing in case both base and top
  point to a single slot (e.g. function call with no arguments given).

As a result of these changes red zone slots are dumped unconditionally
on the top of the stack dump and top slot is unwound like the occupied
slots are. The only artefact of the latter is the possible garbage
printed as the top slot value. One can find the new example of
<lj-stack> output below:

| (gdb) lj-stack L
| ----------- Red zone:  5 slots -----------
| 0x40047fc0            [    ] VALUE: nil
| 0x40047fb8            [    ] VALUE: nil
| 0x40047fb0            [    ] VALUE: nil
| 0x40047fa8            [    ] VALUE: nil
| 0x40047fa0            [    ] VALUE: nil
| ----------- Stack:   168 slots -----------
| 0x40047f98            [   M] VALUE: nil
| 0x40047ae0:0x40047f90 [    ] 151 slots: Free stack slots
| 0x40047ad8            [ BT ] VALUE: number 2.3873123941281106e-313
| 0x40047ad0            [    ] FRAME: [PP] delta=1, fast function #27
| 0x40047ac8            [    ] FRAME: [L] delta=7, fast function #20
| 0x40047ac0            [    ] VALUE: Lua function @ 0x400c00b0, 2 upvalues, "@builtin/box/console.lua":243
| 0x40047ab8            [    ] VALUE: nil
| 0x40047ab0            [    ] VALUE: Lua function @ 0x4014bb78, 0 upvalues, "return collectgarbage()":0
| 0x40047aa8            [    ] VALUE: nil
| 0x40047aa0            [    ] VALUE: string "collectgarbage()" @ 0x40116660
| 0x40047a98            [    ] VALUE: table @ 0x400c0f88 (asize: 0, hmask: 0x1)
| 0x40047a90            [    ] FRAME: [LP] delta=3, Lua function @ 0x4014fe30, 3 upvalues, "@builtin/box/console.lua":379
| 0x40047a88            [    ] VALUE: string "collectgarbage()" @ 0x40116660
| 0x40047a80            [    ] VALUE: table @ 0x400c0f88 (asize: 0, hmask: 0x1)
| 0x40047a78            [    ] FRAME: [L] delta=3, Lua function @ 0x40075aa8, 1 upvalues, "@builtin/box/console.lua":701
| 0x40047a70            [    ] VALUE: string "/home/imun" @ 0x400ff388
| 0x40047a68            [    ] VALUE: table @ 0x400c0f88 (asize: 0, hmask: 0x1)
| 0x40047a60            [    ] FRAME: [CP] delta=1, Lua function @ 0x4014d0c8, 5 upvalues, "@builtin/box/console.lua":746
| 0x40047a58            [S   ] FRAME: dummy L

Signed-off-by: Igor Munkin <imun at tarantool.org>
---
 src/luajit-gdb.py | 32 +++++++++++++++++---------------
 1 file changed, 17 insertions(+), 15 deletions(-)

diff --git a/src/luajit-gdb.py b/src/luajit-gdb.py
index 4488775c..758ba10c 100644
--- a/src/luajit-gdb.py
+++ b/src/luajit-gdb.py
@@ -391,47 +391,49 @@ def dump_framelink(L, fr):
         f = dump_lj_tfunc(fr),
     )
 
-def dump_stack_slot(L, slot, base=None, top=None):
+def dump_stack_slot(L, slot, base=None, top=None, eol='\n'):
     base = base or L['base']
     top = top or L['top']
 
-    return '{addr}{padding} [ {B}{T}{M}] VALUE: {value}\n'.format(
+    return '{addr}{padding} [ {B}{T}{M}] VALUE: {value}{eol}'.format(
         addr = strx64(slot),
         padding = PADDING,
         B = 'B' if slot == base else ' ',
         T = 'T' if slot == top else ' ',
         M = 'M' if slot == mref('TValue *', L['maxstack']) else ' ',
         value = dump_tvalue(slot),
+        eol = eol,
     )
 
 def dump_stack(L, base=None, top=None):
     base = base or L['base']
     top = top or L['top']
+    stack = mref('TValue *', L['stack'])
     maxstack = mref('TValue *', L['maxstack'])
     red = 5 + 2 * LJ_FR2
 
     dump = '\n'.join([
-        '{start}:{end} [    ] {n} slots: Red zone'.format(
-             start = strx64(maxstack + 1),
-             end = strx64(maxstack + red),
-             n = red,
+        '{padding} Red zone: {nredslots: >2} slots {padding}'.format(
+            padding = '-' * len(PADDING),
+            nredslots = red,
         ),
-        '{maxstack}{padding} [   M]'.format(
-            maxstack = strx64(maxstack),
-            padding = PADDING,
+        *(
+            dump_stack_slot(L, maxstack + offset, base, top, '')
+                for offset in range(red, 0, -1)
         ),
+        '{padding} Stack: {nstackslots: >5} slots {padding}'.format(
+            padding = '-' * len(PADDING),
+            nstackslots = int((tou64(maxstack) - tou64(stack)) >> 3),
+        ),
+        dump_stack_slot(L, maxstack, base, top, ''),
         '{start}:{end} [    ] {nfreeslots} slots: Free stack slots'.format(
             start = strx64(top + 1),
             end = strx64(maxstack - 1),
             nfreeslots = int((tou64(maxstack) - tou64(top) - 8) >> 3),
         ),
-        '{top}{padding} [  T ]'.format(
-            top = strx64(top),
-            padding = PADDING,
-        )
     ]) + '\n'
 
-    slot = top - 1
+    slot = top
     framelink = base - (1 + LJ_FR2)
 
     # XXX: Lua stack unwinding algorithm consists of the following steps:
@@ -447,7 +449,7 @@ def dump_stack(L, base=None, top=None):
         dump += dump_stack_slot(L, slot, base, top)
         slot -= 1
 
-    while framelink > mref('TValue *', L['stack']):
+    while framelink > stack:
         assert slot == framelink + LJ_FR2, "Invalid slot during frame unwind"
         dump += dump_framelink(L, framelink)
         framelink = frame_prev(framelink + LJ_FR2) - LJ_FR2
-- 
2.34.0



More information about the Tarantool-patches mailing list