From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 534D96FC83; Fri, 20 Aug 2021 14:11:12 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 534D96FC83 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1629457872; bh=tqa7XcYKsBW71iCHujX1Lc8UIJW/gExgcbrInlqjjw4=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=h55b9uJwXH2AkBPwv3VeoGjjBvMpLxuVsWp0rulKyYKF3A+D8zGR/GY3k/RI1KzgK NvfduH9l0p5ftbEqMJx3SzoMBKnJQTEUAhZzv63gntD/Ub+Zgk9lRIAzCcN66yxYaY wPeDbubnZ0bo9Js7iQ7bjR68OfhAiy3jDAvNZOp8= Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com [209.85.167.53]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 80ABE6DB02 for ; Fri, 20 Aug 2021 14:10:43 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 80ABE6DB02 Received: by mail-lf1-f53.google.com with SMTP id i28so19858000lfl.2 for ; Fri, 20 Aug 2021 04:10:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=H5vjzTF0ef4RgzKFhcub4WADX2dDG5/ReDU2tkIA4yk=; b=hY9pF9p+sLiVTiKKQzW+6sxrMIy1UDa6GqpMEqYtwUmQE4UDcK7VSVAGgzwyilJnb7 SWsLlbod9QKgKXkKz3g08brdkgfEbe9cxo2fsAA5FBj0RA2DEHBAAR5v2bxBT5L7H8u2 JJlSA5n1jyP40r+hzAaj47MRNA+zo+KpQbJ8mRAhbpE26uYQdFF1BnoufMXJXqaadXyd H/kLIjBWzXF2YwD8ZSp8LseGKTHOgNQWLmjTG7wWwI8WZdkYUBtSNqoYh4mfNZg817vs kVuPHsVQ0LviReQJhaa2yLGZ56MyudpIlG+EqbtBLSOfEXi2KmWiXAU7oNhSWljjKtCw 4Ztg== X-Gm-Message-State: AOAM533mkxYs2OdCTN2Auv2iMopWEx7BfO7LuyYeg6swzBEGDw1Cr6aG ysdgQrJ+3pO0W93LEW4e9Dljen67MO/8aXB6 X-Google-Smtp-Source: ABdhPJy6zi5WLiScKjuUkjn6ZpL18xgnZJ1GBG4BMzxGA5MGHXrOgmppY825M+2v5ptmSuKf+g8Ppw== X-Received: by 2002:a05:6512:10cb:: with SMTP id k11mr14205700lfg.651.1629457842803; Fri, 20 Aug 2021 04:10:42 -0700 (PDT) Received: from localhost.localdomain ([93.175.2.170]) by smtp.gmail.com with ESMTPSA id k15sm183951lfv.141.2021.08.20.04.10.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Aug 2021 04:10:42 -0700 (PDT) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, imun@tarantool.org, skaplun@tarantool.org Date: Fri, 20 Aug 2021 14:10:33 +0300 Message-Id: X-Mailer: git-send-email 2.32.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit v2 2/3] memprof: extend symtab with information about .so libs X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Maxim Kokryashkin via Tarantool-patches Reply-To: Maxim Kokryashkin Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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 +#include +#include +#include #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