Tarantool development patches archive
 help / color / mirror / Atom feed
From: Maxim Kokryashkin via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: tarantool-patches@dev.tarantool.org, imun@tarantool.org,
	skaplun@tarantool.org
Subject: [Tarantool-patches] [PATCH luajit v2 2/3] memprof: extend symtab with information about .so libs
Date: Fri, 20 Aug 2021 14:10:33 +0300	[thread overview]
Message-ID: <aca0e144c9c0f24e27d7e5e13842bdd0acbfb297.1629457244.git.m.kokryashkin@tarantool.org> (raw)
In-Reply-To: <cover.1629457244.git.m.kokryashkin@tarantool.org>

This commit enriches memprof's symbol table with information about loaded
".so" files. That information will provide demangling capabilities to
the parser.

The following data is stored for each ".so" library:
| SYMTAB_SO | path to .so | it's address |  CRC32 Hash  |
  1 byte                       8 bytes       8 bytes
  magic
  number

C symbol address from profile events with an address of the ".so" file will
give us an offset, and it can be used to get the symbol in that file.

CRC32 hash will be used by the parser to determine whether the shared
object that was used during the process profiling and during the process
of parsing is the same.

Part of tarantool/tarantool#5813
---
 src/lj_memprof.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/lj_memprof.h |  3 ++-
 2 files changed, 62 insertions(+), 1 deletion(-)

diff --git a/src/lj_memprof.c b/src/lj_memprof.c
index 2c1ef3b8..c8367c95 100644
--- a/src/lj_memprof.c
+++ b/src/lj_memprof.c
@@ -5,10 +5,15 @@
 ** Copyright (C) 2015-2019 IPONWEB Ltd.
 */
 
+#define _GNU_SOURCE
+
 #define lj_memprof_c
 #define LUA_CORE
 
 #include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <link.h>
 
 #include "lj_arch.h"
 #include "lj_memprof.h"
@@ -18,12 +23,64 @@
 #include "lj_obj.h"
 #include "lj_frame.h"
 #include "lj_debug.h"
+#include "lj_utils.h"
 
 /* --------------------------------- Symtab --------------------------------- */
 
 static const unsigned char ljs_header[] = {'l', 'j', 's', LJS_CURRENT_VERSION,
 					   0x0, 0x0, 0x0};
 
+int lj_file_crc32(const char* name, uint32_t* result) 
+{
+#define CRC_BUF_SIZE 1024 * 1024
+
+  char buf[CRC_BUF_SIZE] = "";
+  size_t read_bytes = 0;
+  FILE* so = NULL;
+
+  if (result == NULL) {
+    return 1;
+  }
+
+  *result = 0xffffffff;
+
+  so = fopen(name, "rb");
+  if (!so)
+    return 1;
+
+  while (CRC_BUF_SIZE == (read_bytes = fread(buf, sizeof(*buf), CRC_BUF_SIZE, so))) {
+    *result = lj_utils_crc32(buf, CRC_BUF_SIZE, *result); 
+  }
+  
+  if(ferror(so) || !feof(so)) {
+    fclose(so);
+    return 1;
+  }
+  
+  *result = lj_utils_crc32(buf, read_bytes, *result);
+  fclose(so);
+  return 0;
+}
+
+int write_shared_obj(struct dl_phdr_info *info, size_t size, void *data)
+{
+  uint32_t so_hash = 0;
+  struct lj_wbuf *buf = data;
+
+  if (info->dlpi_name && strlen(info->dlpi_name)) {
+    if (0 != lj_file_crc32(info->dlpi_name, &so_hash))
+      // XXX: Maybe it is reasonable to print warning here
+      return 0;
+
+    lj_wbuf_addbyte(buf, SYMTAB_SO);
+    lj_wbuf_addstring(buf, info->dlpi_name);
+    lj_wbuf_addu64(buf, info->dlpi_addr);
+    lj_wbuf_addu64(buf, so_hash);
+  }
+
+  return 0;
+}
+
 static void dump_symtab(struct lj_wbuf *out, const struct global_State *g)
 {
   const GCRef *iter = &g->gc.root;
@@ -49,6 +106,9 @@ static void dump_symtab(struct lj_wbuf *out, const struct global_State *g)
     iter = &o->gch.nextgc;
   }
 
+  /* Write shared libraries. */
+  dl_iterate_phdr(write_shared_obj, out);
+
   lj_wbuf_addbyte(out, SYMTAB_FINAL);
 }
 
diff --git a/src/lj_memprof.h b/src/lj_memprof.h
index 3417475d..0cefc403 100644
--- a/src/lj_memprof.h
+++ b/src/lj_memprof.h
@@ -16,7 +16,7 @@
 #include "lj_def.h"
 #include "lj_wbuf.h"
 
-#define LJS_CURRENT_VERSION 0x1
+#define LJS_CURRENT_VERSION 0x2
 
 /*
 ** symtab format:
@@ -51,6 +51,7 @@
 */
 
 #define SYMTAB_LFUNC ((uint8_t)0)
+#define SYMTAB_SO    ((uint8_t)1)
 #define SYMTAB_FINAL ((uint8_t)0x80)
 
 #define LJM_CURRENT_FORMAT_VERSION 0x01
-- 
2.32.0


  parent reply	other threads:[~2021-08-20 11:11 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-20 11:10 [Tarantool-patches] [PATCH luajit v2 0/3] memprof: add demangling capabilities for C functions Maxim Kokryashkin via Tarantool-patches
2021-08-20 11:10 ` [Tarantool-patches] [PATCH luajit v2 1/3] utils: add CRC32 hash implementation Maxim Kokryashkin via Tarantool-patches
2021-08-30 14:08   ` Sergey Kaplun via Tarantool-patches
2021-08-20 11:10 ` Maxim Kokryashkin via Tarantool-patches [this message]
2021-08-30 14:10   ` [Tarantool-patches] [PATCH luajit v2 2/3] memprof: extend symtab with information about .so libs Sergey Kaplun via Tarantool-patches
2021-08-20 11:10 ` [Tarantool-patches] [PATCH luajit v2 3/3] memprof: update memprof parser Maxim Kokryashkin via Tarantool-patches
2021-08-25 10:01 ` [Tarantool-patches] [PATCH luajit v2 0/3] memprof: add demangling capabilities for C functions Максим Корякшин 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=aca0e144c9c0f24e27d7e5e13842bdd0acbfb297.1629457244.git.m.kokryashkin@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imun@tarantool.org \
    --cc=max.kokryashkin@gmail.com \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit v2 2/3] memprof: extend symtab with information about .so libs' \
    /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