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 1/2] symtab: fix static symtab dump
Date: Mon,  4 Jul 2022 00:38:14 +0300	[thread overview]
Message-ID: <56629a2a9ec830a80fef615d3848ef3535bf2b2b.1656883823.git.m.kokryashkin@tarantool.org> (raw)
In-Reply-To: <cover.1656883823.git.m.kokryashkin@tarantool.org>

The `dl_iterate_phdr` returns an empty string as a name for
the executable from which it was called. It is still possible
to access its dynamic symbol table, but it is vital for
sysprof to obtain the main symbol table for the LuaJIT
executable. To do so, we need a valid path to the executable.

Since there is no way to obtain the path to a running executable
using the C standard library, this commit adds call to
`readlink` to gather the symbolic link from `/proc/self/exe`.
Most of the UNIX-based systems have procfs, so it is not
a problem.

Needed for tarantool/tarantool#7244
---
 src/lj_symtab.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/src/lj_symtab.c b/src/lj_symtab.c
index 2b2b205b..23696401 100644
--- a/src/lj_symtab.c
+++ b/src/lj_symtab.c
@@ -16,10 +16,12 @@
 
 #if LJ_HASRESOLVER
 
+#include <linux/limits.h>
 #include <elf.h>
 #include <link.h>
 #include <stdio.h>
 #include <sys/auxv.h>
+#include <unistd.h>
 #include "lj_gc.h"
 #endif
 
@@ -352,6 +354,7 @@ static int resolve_symbolnames(struct dl_phdr_info *info, size_t info_size,
   struct lj_wbuf *buf = conf->buf;
   lua_State *L = conf->L;
   const uint8_t header = conf->header;
+  char executable_path[PATH_MAX] = "";
 
   uint32_t lib_cnt = 0;
 
@@ -387,6 +390,31 @@ static int resolve_symbolnames(struct dl_phdr_info *info, size_t info_size,
   ** Main way: try to open ELF and read SHT_SYMTAB, SHT_STRTAB and SHT_HASH
   ** sections from it.
   */
+
+  /*
+  ** The `dl_iterate_phdr` returns an empty string as a name for
+  ** the executable from which it was called. It is still possible to
+  ** access its dynamic symbol table, but it is vital for sysprof to obtain
+  ** the main symbol table for the LuaJIT executable. To do so, we need a
+  ** valid path to the executable. Since there is no way to obtain the
+  ** path to a running executable using the C standard library, the only
+  ** more or less reliable way to do this is by reading the symbolic link
+  ** from `/proc/self/exe`. Most of the UNIX-based systems have procfs, so
+  ** it is not a problem.
+  */
+  if (*info->dlpi_name == 0) {
+    if (-1 != readlink("/proc/self/exe", executable_path, PATH_MAX))
+      info->dlpi_name = executable_path;
+    else
+      /*
+      ** It is impossible for sysprof to function properly without the
+      ** LuaJIT's .symtab section present. The assertion below
+      ** is unlikely to be triggered on any system supported by sysprof,
+      ** unless someone have deleted the LuaJIT binary right after the
+      ** start.
+      */
+      lua_assert(0);
+  }
   if (dump_sht_symtab(info->dlpi_name, buf, L, header, info->dlpi_addr) == 0) {
     ++conf->cur_lib;
   }
-- 
2.36.1


  reply	other threads:[~2022-07-03 21:38 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-03 21:38 [Tarantool-patches] [PATCH luajit 0/2] sysprof: fix inconsistencies Maxim Kokryashkin via Tarantool-patches
2022-07-03 21:38 ` Maxim Kokryashkin via Tarantool-patches [this message]
2022-07-16  7:21   ` [Tarantool-patches] [PATCH luajit 1/2] symtab: fix static symtab dump Sergey Kaplun via Tarantool-patches
2022-07-03 21:38 ` [Tarantool-patches] [PATCH luajit 2/2] sysprof: add stack sandwich support Maxim Kokryashkin via Tarantool-patches
2022-07-16  7:32   ` Sergey Kaplun via Tarantool-patches
2022-07-18 17:23 ` [Tarantool-patches] [PATCH luajit 0/2] sysprof: fix inconsistencies Igor Munkin via Tarantool-patches
2022-08-10 14:34 ` Igor Munkin 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=56629a2a9ec830a80fef615d3848ef3535bf2b2b.1656883823.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 1/2] symtab: fix static symtab dump' \
    /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