Tarantool development patches archive
 help / color / mirror / Atom feed
From: Mikhail Elhimov via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Sergey Kaplun <skaplun@tarantool.org>,
	Sergey Bronnikov <sergeyb@tarantool.org>,
	Evgeniy Temirgaleev <e.temirgaleev@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH luajit v2 1/7] dbg: simplify composing of all commands output
Date: Fri,  4 Dec 2026 23:03:02 +0300	[thread overview]
Message-ID: <7d2100c059827ef3ccc67b24652e5f3da0631d11.1796410322.git.m.elhimov@vk.team> (raw)
In-Reply-To: <cover.1796410322.git.m.elhimov@vk.team>

* removed trailing newlines in all the dumpers to unify (and simplify)
  composing of commands output
* introduced `dbg.print` and used wherever trailing newline is needed to
  make more compact and clear code
* adjusted `dump_jslot` to accept only concrete dumpee value to make
  code more readable (omitted argument of the corresponding command is
  handled outside of the dumper)
---
 src/luajit_dbg.py | 102 ++++++++++++++++++++--------------------------
 1 file changed, 45 insertions(+), 57 deletions(-)

diff --git a/src/luajit_dbg.py b/src/luajit_dbg.py
index 80057a4e..f498ef3d 100644
--- a/src/luajit_dbg.py
+++ b/src/luajit_dbg.py
@@ -110,6 +110,10 @@ class Debugger(object):
                 self.write('{} command initialized\n'.format(name))
             self.write('LuaJIT debug extension is successfully loaded\n')
 
+    def print(self, msg):
+        '''Print the message with the new line.'''
+        self.write(msg + '\n')
+
     @abc.abstractmethod
     def cast(self, typestr, val):
         '''Cast the value to the required C type.'''
@@ -2399,7 +2403,7 @@ def dump_proto(proto):
     startbc = proto_bc(proto)
     func_loc = proto_loc(proto)
     # Location has the following format: '{chunk}:{firstline}'.
-    dump = '{func_loc}-{lastline}\n'.format(
+    dump = '{func_loc}-{lastline}'.format(
         func_loc=func_loc,
         lastline=proto['firstline'] + proto['numline'],
     )
@@ -2408,9 +2412,9 @@ def dump_proto(proto):
         return '=> ' + str(npc_from + delta).zfill(4)
 
     for bcnum in range(0, int(proto['sizebc'])):
-        dump += (str(bcnum).zfill(4) + ' ' + dump_bc(
+        dump += ('\n' + str(bcnum).zfill(4) + ' ' + dump_bc(
             startbc[bcnum], jmp_format=jmp_format, jmp_ctx=bcnum, proto=proto,
-        ) + '\n')
+        ))
     return dump
 
 
@@ -2421,9 +2425,9 @@ def dump_func(func):
         pt = funcproto(func)
         return dump_proto(pt)
     elif ffid == 1:
-        return 'C function @ {}\n'.format(strx64(func['f']))
+        return 'C function @ {}'.format(strx64(func['f']))
     else:
-        return 'fast function #{}\n'.format(int(ffid))
+        return 'fast function #{}'.format(int(ffid))
 
 
 # FFI dumpers.
@@ -2740,7 +2744,7 @@ def dump_irins(irins, trace=None):
         if op1 != -1:
             args = dump_call_args(trace, int(op1))
 
-        return '{flags} {type} {name:6} [{mode:2}] {f}({args}) {ct}\n'.format(
+        return '{flags} {type} {name:6} [{mode:2}] {f}({args}) {ct}'.format(
             flags=flags,
             name=irname,
             mode=ir_mode(irop),
@@ -2792,7 +2796,7 @@ def dump_irins(irins, trace=None):
     else:
         typename = IRTYPES[irt_type(irt)]
 
-    return '{flags} {type} {name:6} [{mode:2}] {left:<9s} {right}\n'.format(
+    return '{flags} {type} {name:6} [{mode:2}] {left:<9s} {right}'.format(
         flags=flags,
         name=irname,
         mode=ir_mode(irop),
@@ -2857,18 +2861,19 @@ def dump_regsp(irins, ins_number):
 
 
 def dump_trace(trace, flags):
-    dump = 'Trace {num} start\n\tproto: {start_pt}\n\tBC: {start_bc}\n'.format(
+    dump = 'Trace {num} start\n\tproto: {start_pt}\n\tBC: {start_bc}'.format(
         num=trace['traceno'],
         start_pt=gcref(trace['startpt']),
         start_bc=mref('BCIns *', trace['startpc']),
     )
 
     nins = trace['nins'] - REF_BIAS
-    dump += '---- TRACE IR\n'
+    dump += '\n---- TRACE IR'
     nsnap = 0
     snap = trace['snap'][nsnap]
     snapref = snap['ref']
     for irnum in range(1, nins):
+        dump += '\n'
         irref = REF_BIAS + irnum
         if 's' in flags and irref >= snapref and nsnap < trace['nsnap']:
             dump += '....          '
@@ -2894,27 +2899,23 @@ def dump_tref(tref):
     )
 
 
-def dump_jslots(coroutine):
-    lstate = L(None)
-    g = G(lstate or coroutine)
-    j = J(g)
-
-    dump = ''
-    maxslot = j['baseslot'] + j['maxslot']
+def dump_jslots(J):
+    dump = []
+    maxslot = J['baseslot'] + J['maxslot']
     first_base_slot = 1 + LJ_FR2
     for n in reversed(range(first_base_slot, maxslot)):
-        tref = j['slot'][n]
+        tref = J['slot'][n]
         ref = tref_ref(tref)
         address = dbg.address(tref)
-        dump += '{addr} {nslot:04d} {base:1s} {tref}{const}\n'.format(
+        dump.append('{addr} {nslot:04d} {base:1s} {tref}{const}'.format(
             addr=address,
-            base='B' if address == j['base'] else ' ',
+            base='B' if address == J['base'] else ' ',
             nslot=n,
             tref=dump_tref(tref),
-            const=' ' + dump_irk(j['cur'], ref - REF_BIAS)
-                if ref != 0 and ref < REF_BIAS else ''
-        )
-    return dump
+            const=' ' + dump_irk(J['cur'], ref - REF_BIAS)
+                  if ref != 0 and ref < REF_BIAS else ''
+        ))
+    return '\n'.join(dump)
 
 
 # Extension commands. ############################################
@@ -2931,8 +2932,8 @@ compile-time flag to inspect if LuaJIT is built in dual-number mode.
     '''
 
     def execute(self, arg):
-        dbg.write(
-            'LJ_64: {LJ_64}, LJ_GC64: {LJ_GC64}, LJ_DUALNUM: {LJ_DUALNUM}\n'
+        dbg.print(
+            'LJ_64: {LJ_64}, LJ_GC64: {LJ_GC64}, LJ_DUALNUM: {LJ_DUALNUM}'
             .format(
                 LJ_64=LJ_64,
                 LJ_GC64=LJ_GC64,
@@ -2964,9 +2965,7 @@ https://github.com/tarantool/tarantool/wiki/LuaJIT-Bytecodes.
     '''
 
     def execute(self, arg):
-        dbg.write('{}\n'.format(
-            dump_bc(dbg.cast('BCIns *', dbg.eval(arg))[0])
-        ))
+        dbg.print(dump_bc(dbg.cast('BCIns *', dbg.eval(arg))[0]))
 
 
 class LJDumpCType(dbg.LJBase):
@@ -2978,9 +2977,7 @@ and dumps the ID and the name for this C data type.
     '''
 
     def execute(self, arg):
-        dbg.write('{}\n'.format(
-            dump_ctype(dbg.cast('CType *', dbg.eval(arg)))
-        ))
+        dbg.print(dump_ctype(dbg.cast('CType *', dbg.eval(arg))))
 
 
 class LJDumpFunc(dbg.LJBase):
@@ -3005,7 +3002,7 @@ function:
     '''
 
     def execute(self, arg):
-        dbg.write('{}'.format(dump_func(dbg.cast('GCfuncC *', dbg.eval(arg)))))
+        dbg.print(dump_func(dbg.cast('GCfuncC *', dbg.eval(arg))))
 
 
 class LJGC(dbg.LJBase):
@@ -3029,7 +3026,7 @@ The command requires no args and dumps current GC stats:
 
     def execute(self, arg):
         g = G(L(None))
-        dbg.write('GC stats: {state}\n{stats}\n'.format(
+        dbg.print('GC stats: {state}\n{stats}'.format(
             state=gc_state(g),
             stats=dump_gc(g)
         ))
@@ -3060,8 +3057,7 @@ error message occurs.
     '''
 
     def execute(self, arg):
-        gcobj = dbg.cast('GCobj *', dbg.eval(arg))
-        dbg.write('{}\n'.format(dump_gcobj(gcobj)))
+        dbg.print(dump_gcobj(dbg.cast('GCobj *', dbg.eval(arg))))
 
 
 class LJDumpIR(dbg.LJBase):
@@ -3078,7 +3074,7 @@ https://github.com/tarantool/tarantool/wiki/LuaJIT-SSA-IR.
     '''
 
     def execute(self, arg):
-        dbg.write('{}'.format(dump_irins(dbg.cast('IRIns *', dbg.eval(arg)))))
+        dbg.print(dump_irins(dbg.cast('IRIns *', dbg.eval(arg))))
 
 
 class LJDumpJSlots(dbg.LJBase):
@@ -3095,9 +3091,7 @@ when there is no coroutine to be inspected in the debugged frame.
     '''
 
     def execute(self, arg):
-        dbg.write('{}'.format(
-            dump_jslots(dbg.cast('lua_State *', dbg.eval(arg)))
-        ))
+        dbg.print(dump_jslots(J(G(L(dbg.eval(arg))))))
 
 
 class LJDumpProto(dbg.LJBase):
@@ -3122,9 +3116,7 @@ function:
     '''
 
     def execute(self, arg):
-        dbg.write('{}'.format(
-            dump_proto(dbg.cast('GCproto *', dbg.eval(arg)))
-        ))
+        dbg.print(dump_proto(dbg.cast('GCproto *', dbg.eval(arg))))
 
 
 class LJDumpStack(dbg.LJBase):
@@ -3161,7 +3153,7 @@ If L is omitted, the main coroutine is used.
     '''
 
     def execute(self, arg):
-        dbg.write('{}\n'.format(dump_stack(L(dbg.eval(arg)))))
+        dbg.print(dump_stack(L(dbg.eval(arg))))
 
 
 class LJState(dbg.LJBase):
@@ -3175,13 +3167,13 @@ The command requires no args and dumps current VM and GC states:
 
     def execute(self, arg):
         g = G(L(None))
-        dbg.write('{}\n'.format('\n'.join(
+        dbg.print('\n'.join(
             map(lambda t: '{} state: {}'.format(*t), {
                 'VM':  vm_state(g),
                 'GC':  gc_state(g),
                 'JIT': jit_state(g),
             }.items())
-        )))
+        ))
 
 
 class LJDumpString(dbg.LJBase):
@@ -3197,7 +3189,7 @@ is replaced with the corresponding error when decoding fails.
 
     def execute(self, arg):
         string = dbg.cast('GCstr *', dbg.eval(arg))
-        dbg.write("String: {body} [{len} bytes] with hash {hash}\n".format(
+        dbg.print("String: {body} [{len} bytes] with hash {hash}".format(
             body=strdata(string),
             hash=strx64(string['hash']),
             len=string['len'],
@@ -3227,22 +3219,22 @@ The command receives a GCtab address and dumps the table contents:
         }
 
         if mt != 0:
-            dbg.write('Metatable detected: {}\n'.format(strx64(mt)))
+            dbg.print('Metatable detected: {}'.format(strx64(mt)))
 
-        dbg.write('Array part: {} slots\n'.format(capacity['apart']))
+        dbg.print('Array part: {} slots'.format(capacity['apart']))
         for i in range(capacity['apart']):
             slot = array + i
-            dbg.write('{ptr}: [{index}]: {value}\n'.format(
+            dbg.print('{ptr}: [{index}]: {value}'.format(
                 ptr=slot,
                 index=i,
                 value=dump_tvalue(slot)
             ))
 
-        dbg.write('Hash part: {} nodes\n'.format(capacity['hpart']))
+        dbg.print('Hash part: {} nodes'.format(capacity['hpart']))
         # See hmask comment in lj_obj.h
         for i in range(capacity['hpart']):
             node = nodes + i
-            dbg.write('{ptr}: {{ {key} }} => {{ {val} }}; next = {n}\n'.format(
+            dbg.print('{ptr}: {{ {key} }} => {{ {val} }}; next = {n}'.format(
                 ptr=node,
                 key=dump_tvalue(node['key']),
                 val=dump_tvalue(node['val']),
@@ -3280,8 +3272,7 @@ error message occurs.
     '''
 
     def execute(self, arg):
-        tv = dbg.cast('TValue *', dbg.eval(arg))
-        dbg.write('{}\n'.format(dump_tvalue(tv)))
+        dbg.print(dump_tvalue(dbg.cast('TValue *', dbg.eval(arg))))
 
 
 class LJDumpTrace(dbg.LJBase):
@@ -3300,10 +3291,7 @@ Trace may be preceded with /FLAGS:
 
     def execute(self, arg):
         arg, flags = dbg.extract_flags(arg, 'rs')
-        dbg.write('{}'.format(dump_trace(
-            dbg.cast('GCtrace *', dbg.eval(arg)),
-            flags
-        )))
+        dbg.print(dump_trace(dbg.cast('GCtrace *', dbg.eval(arg)), flags))
 
 
 def load(event=None):
-- 
2.55.0


  reply	other threads:[~2026-12-04 20:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 20:03 [Tarantool-patches] [PATCH luajit v2 0/7] dbg: avoid hardcoded enums Mikhail Elhimov via Tarantool-patches
2026-12-04 20:03 ` Mikhail Elhimov via Tarantool-patches [this message]
2026-12-04 20:03 ` [Tarantool-patches] [PATCH luajit v2 2/7] dbg: cleanup and unify `dbg.eval` Mikhail Elhimov via Tarantool-patches
2026-12-04 20:03 ` [Tarantool-patches] [PATCH luajit v2 3/7] dbg: handle missing cmd arg in a dbg-agnostic way Mikhail Elhimov via Tarantool-patches
2026-12-04 20:03 ` [Tarantool-patches] [PATCH luajit v2 4/7] dbg: add tests of deferred extension loading Mikhail Elhimov via Tarantool-patches
2026-12-04 20:03 ` [Tarantool-patches] [PATCH luajit v2 5/7] dbg: avoid hardcoded enums Mikhail Elhimov via Tarantool-patches
2026-12-04 20:03 ` [Tarantool-patches] [PATCH luajit v2 6/7] dbg: display fast function name along with ffid Mikhail Elhimov via Tarantool-patches
2026-12-04 20:03 ` [Tarantool-patches] [PATCH luajit v2 7/7] dbg: remove wrong GC state Mikhail Elhimov via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7d2100c059827ef3ccc67b24652e5f3da0631d11.1796410322.git.m.elhimov@vk.team \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=e.temirgaleev@tarantool.org \
    --cc=m.elhimov@vk.team \
    --cc=sergeyb@tarantool.org \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit v2 1/7] dbg: simplify composing of all commands output' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox