[Tarantool-patches] [PATCH luajit v1 04/11] profile: introduce symtab write module

Sergey Kaplun skaplun at tarantool.org
Wed Dec 16 22:13:39 MSK 2020


This patch adds profile writer that writes all necessary Lua
functions prototypes info like GCproto address, name of the chunk this
function was defined in and number of the first line of it.
See <ljp_symtab.h> for details.

Usage of this module will be added at the next patches.

Part of tarantool/tarantool#5442
---
 src/Makefile             |  2 +-
 src/Makefile.dep         |  2 ++
 src/profile/ljp_symtab.c | 55 ++++++++++++++++++++++++++++++++++++++
 src/profile/ljp_symtab.h | 57 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 115 insertions(+), 1 deletion(-)
 create mode 100644 src/profile/ljp_symtab.c
 create mode 100644 src/profile/ljp_symtab.h

diff --git a/src/Makefile b/src/Makefile
index 4b1d937..e00265c 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -469,7 +469,7 @@ DASM_FLAGS= $(DASM_XFLAGS) $(DASM_AFLAGS)
 DASM_DASC= vm_$(DASM_ARCH).dasc
 
 UTILS_O= utils/leb128.o
-PROFILE_O= profile/ljp_write.o
+PROFILE_O= profile/ljp_write.o profile/ljp_symtab.o
 BUILDVM_O= host/buildvm.o host/buildvm_asm.o host/buildvm_peobj.o \
 	   host/buildvm_lib.o host/buildvm_fold.o
 BUILDVM_T= host/buildvm
diff --git a/src/Makefile.dep b/src/Makefile.dep
index 7fdbfbe..831a5ce 100644
--- a/src/Makefile.dep
+++ b/src/Makefile.dep
@@ -248,6 +248,8 @@ host/buildvm_lib.o: host/buildvm_lib.c host/buildvm.h lj_def.h lua.h luaconf.h \
 host/buildvm_peobj.o: host/buildvm_peobj.c host/buildvm.h lj_def.h lua.h \
  luaconf.h lj_arch.h lj_bc.h lj_def.h lj_arch.h
 host/minilua.o: host/minilua.c
+profile/ljp_symtab.o: profile/ljp_symtab.c lj_obj.h lua.h luaconf.h lj_def.h \
+ lj_arch.h profile/ljp_write.h profile/ljp_symtab.h
 profile/ljp_write.o: profile/ljp_write.c profile/ljp_write.h utils/leb128.h \
  lj_def.h lua.h luaconf.h
 utils/leb128.o: utils/leb128.c
diff --git a/src/profile/ljp_symtab.c b/src/profile/ljp_symtab.c
new file mode 100644
index 0000000..5a17c97
--- /dev/null
+++ b/src/profile/ljp_symtab.c
@@ -0,0 +1,55 @@
+/*
+** Implementation of the Lua symbol table dumper.
+**
+** Major portions taken verbatim or adapted from the LuaVela.
+** Copyright (C) 2015-2019 IPONWEB Ltd.
+*/
+
+#include "lj_obj.h"
+#include "profile/ljp_write.h"
+#include "profile/ljp_symtab.h"
+
+#define LJS_CURRENT_VERSION 2
+
+static const unsigned char ljs_header[] = {'l', 'j', 's', LJS_CURRENT_VERSION,
+					   0x0, 0x0, 0x0};
+
+static void symtab_write_prologue(struct ljp_buffer *out)
+{
+  const size_t len = sizeof(ljs_header) / sizeof(ljs_header[0]);
+  size_t i = 0;
+
+  for (; i < len; i++)
+    ljp_write_byte(out, ljs_header[i]);
+}
+
+void ljp_symtab_write(struct ljp_buffer *out, const struct global_State *g)
+{
+  const GCobj *o;
+  const GCRef *iter = &g->gc.root;
+
+  symtab_write_prologue(out);
+
+  while (NULL != (o = gcref(*iter))) {
+    switch (o->gch.gct) {
+    case (~LJ_TPROTO): {
+      const GCproto *pt = gco2pt(o);
+      ljp_write_byte(out, SYMTAB_LFUNC);
+      ljp_write_u64(out, (uintptr_t)pt);
+      ljp_write_string(out, proto_chunknamestr(pt));
+      ljp_write_u64(out, (uint64_t)pt->firstline);
+      break;
+    }
+    case (~LJ_TTRACE): {
+      /* TODO: Implement dumping a trace info */
+      break;
+    }
+    default: {
+      break;
+    }
+    }
+    iter = &o->gch.nextgc;
+  }
+
+  ljp_write_byte(out, SYMTAB_FINAL);
+}
diff --git a/src/profile/ljp_symtab.h b/src/profile/ljp_symtab.h
new file mode 100644
index 0000000..3a40d98
--- /dev/null
+++ b/src/profile/ljp_symtab.h
@@ -0,0 +1,57 @@
+/*
+** Lua symbol table dumper.
+**
+** Major portions taken verbatim or adapted from the LuaVela.
+** Copyright (C) 2015-2019 IPONWEB Ltd.
+*/
+
+#ifndef _LJ_SYMTAB_H
+#define _LJ_SYMTAB_H
+
+#include <stdint.h>
+
+struct global_State;
+struct ljp_buffer;
+
+/*
+** symtab format:
+**
+** symtab         := prologue sym*
+** prologue       := 'l' 'j' 's' version reserved
+** version        := <BYTE>
+** reserved       := <BYTE> <BYTE> <BYTE>
+** sym            := sym-lua | sym-final
+** sym-lua        := sym-header sym-addr sym-chunk sym-line
+** sym-header     := <BYTE>
+** sym-addr       := <ULEB128>
+** sym-chunk      := string
+** sym-line       := <ULEB128>
+** sym-final      := sym-header
+** string         := string-len string-payload
+** string-len     := <ULEB128>
+** string-payload := <BYTE> {string-len}
+**
+** <BYTE>   :  A single byte (no surprises here)
+** <ULEB128>:  Unsigned integer represented in ULEB128 encoding
+**
+** (Order of bits below is hi -> lo)
+**
+** version: [VVVVVVVV]
+**  * VVVVVVVV: Byte interpreted as a plain numeric version number
+**
+** sym-header: [FUUUUUTT]
+**  * TT    : 2 bits for representing symbol type
+**  * UUUUU : 5 unused bits
+**  * F     : 1 bit marking the end of the symtab (final symbol)
+*/
+
+#define SYMTAB_LFUNC ((uint8_t)0)
+#define SYMTAB_CFUNC ((uint8_t)1)
+#define SYMTAB_FFUNC ((uint8_t)2)
+#define SYMTAB_TRACE ((uint8_t)3)
+#define SYMTAB_FINAL ((uint8_t)0x80)
+
+/* Writes the symbol table of the VM g to out. */
+void ljp_symtab_write(struct ljp_buffer *out, const struct global_State *g);
+
+#endif
-- 
2.28.0



More information about the Tarantool-patches mailing list