Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Kaplun <skaplun@tarantool.org>
To: Igor Munkin <imun@tarantool.org>,
	Sergey Ostanevich <sergos@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH luajit v1 04/11] profile: introduce symtab write module
Date: Wed, 16 Dec 2020 22:13:39 +0300	[thread overview]
Message-ID: <b86b1e8db026e782b2430510eb321b669782a277.1608142899.git.skaplun@tarantool.org> (raw)
In-Reply-To: <cover.1608142899.git.skaplun@tarantool.org>

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

  parent reply	other threads:[~2020-12-16 19:14 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-16 19:13 [Tarantool-patches] [PATCH luajit v1 00/11] LuaJIT memory profiler Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 01/11] build: add src dir in building Sergey Kaplun
2020-12-20 21:27   ` Igor Munkin
2020-12-23 18:20     ` Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 02/11] utils: introduce leb128 reader and writer Sergey Kaplun
2020-12-20 22:44   ` Igor Munkin
2020-12-23 22:34     ` Sergey Kaplun
2020-12-24  9:11       ` Igor Munkin
2020-12-25  8:46         ` Sergey Kaplun
2020-12-23 16:50   ` Sergey Ostanevich
2020-12-23 22:36     ` Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 03/11] profile: introduce profiler writing module Sergey Kaplun
2020-12-21  9:24   ` Igor Munkin
2020-12-24  6:46     ` Sergey Kaplun
2020-12-24 15:45       ` Sergey Ostanevich
2020-12-24 21:20         ` Sergey Kaplun
2020-12-25  9:37           ` Igor Munkin
2020-12-25 10:13             ` Sergey Kaplun
2020-12-16 19:13 ` Sergey Kaplun [this message]
2020-12-21 10:30   ` [Tarantool-patches] [PATCH luajit v1 04/11] profile: introduce symtab write module Igor Munkin
2020-12-24  7:00     ` Sergey Kaplun
2020-12-24  9:36       ` Igor Munkin
2020-12-25  8:45         ` Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 05/11] vm: introduce LFUNC and FFUNC vmstates Sergey Kaplun
2020-12-25 11:07   ` Sergey Ostanevich
2020-12-25 11:23     ` Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 06/11] core: introduce new mem_L field Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 07/11] debug: move debug_frameline to public module API Sergey Kaplun
2020-12-20 22:46   ` Igor Munkin
2020-12-24  6:50     ` Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 08/11] profile: introduce memory profiler Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 09/11] misc: add Lua API for " Sergey Kaplun
2020-12-24 16:32   ` Sergey Ostanevich
2020-12-24 21:25     ` Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 10/11] tools: introduce tools directory Sergey Kaplun
2020-12-20 22:46   ` Igor Munkin
2020-12-24  6:47     ` Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 11/11] profile: introduce profile parser Sergey Kaplun
2020-12-24 23:09   ` Igor Munkin
2020-12-25  8:41     ` Sergey Kaplun
2020-12-21 10:43 ` [Tarantool-patches] [PATCH luajit v1 00/11] LuaJIT memory profiler Igor Munkin
2020-12-24  7:02   ` Sergey Kaplun

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=b86b1e8db026e782b2430510eb321b669782a277.1608142899.git.skaplun@tarantool.org \
    --to=skaplun@tarantool.org \
    --cc=imun@tarantool.org \
    --cc=sergos@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit v1 04/11] profile: introduce symtab write module' \
    /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