From patchwork Thu Nov 11 15:09:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12615037 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A203AC433EF for ; Thu, 11 Nov 2021 15:09:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8A2B8610D0 for ; Thu, 11 Nov 2021 15:09:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233892AbhKKPLz (ORCPT ); Thu, 11 Nov 2021 10:11:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233810AbhKKPLy (ORCPT ); Thu, 11 Nov 2021 10:11:54 -0500 Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22005C061766 for ; Thu, 11 Nov 2021 07:09:05 -0800 (PST) Received: by mail-ed1-x52c.google.com with SMTP id w1so25437217edd.10 for ; Thu, 11 Nov 2021 07:09:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=HvjNhO6rSGOzEcVhxWg2PWcqULJ4m3bhK1jYhtiwVeE=; b=eJCDJUhDZTxYSTZkMt0HP5aNVWp9wRFnSGfarQEm/hBh47EsPN72UL3Hoc6RpaBH4M BV2Kl8raJjFuuW+FPfHDa/ohzJhnwt530h84t2yu9pMKKpOOQbfudHrBQCLUVn9g6c9N Mt8NWyjkt4PAV3w/5GUAPZF6UwWm/k/FDZjl2TpV184fH2sKDFPjoNIO8PwFsg1fIoV/ E0Xvi2FS7Dnd23Iow+W9jZ4OoPdixQ0FdVPI3N++TeZX8aEp3+TLEY3ucWTRaT25n8Ll blVRNOcJ4KuO0ibRwyN8u2hPhvfydhgpa/fye9Qzbud4bDZumGMPUjKS6HZDmYqQlWZm cAMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=HvjNhO6rSGOzEcVhxWg2PWcqULJ4m3bhK1jYhtiwVeE=; b=lRZEPxUsAqi+prtDaUzI4m0dvmaSWqm5YOzyknURfettsLAYt27CQC3fmBfaIZlAKK MGxRfElZ7/SmKcln3EarJ9VY9X0NvYaTLhkgX69AfS4ySQe0qFxX62Vm8iIYOEnK9urw KC6lE0g3++VaT3rYYjVfzipwFYSLZ87r5PJUSJ9+g3hM3T+v7a8L2PJGxgwjUtfkxeve Wvcd8Sv+L9bo0CR8LSA67HGD2ey72dXxHA4EiTPCMvhiRl1/R3YqJUT/wlorQgvTgZ29 FO4oQUfAdHjZM7/FAIKl586FxrtIs9jQjrEThm+ZVKV4F3g68blZYZagP9HMbylz16v2 sFdw== X-Gm-Message-State: AOAM530ZMkOj53qgycFMdX9Bq/hJSvFCy8WrmhqH7UZejJm4qHAe6teW YpZUphkjJlr1D5irD79MbNN8PhChAmTE5A== X-Google-Smtp-Source: ABdhPJwWNrNasDBO5AGwAhELYt+swlc2nCXCPq0vSsIe4ecQND1TWz52DijXTpCzoQCnh6uXDY+I1g== X-Received: by 2002:a17:907:3e25:: with SMTP id hp37mr10364887ejc.43.1636643343688; Thu, 11 Nov 2021 07:09:03 -0800 (PST) Received: from oberon.zico.biz.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id t23sm1590364edc.95.2021.11.11.07.09.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Nov 2021 07:09:02 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v5 04/25] trace-cmd library: Add internal helper function for writing headers before file sections Date: Thu, 11 Nov 2021 17:09:00 +0200 Message-Id: <20211111150900.86585-1-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.33.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Introduce headers before each file section, in trace file version 7. The section header has the following format: <2 bytes>, header ID , null terminated ASCII string, description of the header <2 bytes>, section flags: 1: the section is compressed <4 bytes>, size of the section Signed-off-by: Tzvetomir Stoyanov (VMware) --- include/trace-cmd/trace-cmd.h | 5 ++ .../include/private/trace-cmd-private.h | 1 + lib/trace-cmd/include/trace-cmd-local.h | 5 ++ lib/trace-cmd/trace-output.c | 68 +++++++++++++++++++ 4 files changed, 79 insertions(+) diff --git a/include/trace-cmd/trace-cmd.h b/include/trace-cmd/trace-cmd.h index 7fea4e01..5d71e8ba 100644 --- a/include/trace-cmd/trace-cmd.h +++ b/include/trace-cmd/trace-cmd.h @@ -15,6 +15,11 @@ enum tracecmd_open_flags { TRACECMD_FL_LOAD_NO_PLUGINS = 1 << 0, /* Do not load plugins */ TRACECMD_FL_LOAD_NO_SYSTEM_PLUGINS = 1 << 1, /* Do not load system plugins */ }; + +enum tracecmd_section_flags { + TRACECMD_SEC_FL_COMPRESS = 1 << 0, /* the section is compressed */ +}; + struct tracecmd_input *tracecmd_open_head(const char *file, int flags); struct tracecmd_input *tracecmd_open(const char *file, int flags); struct tracecmd_input *tracecmd_open_fd(int fd, int flags); diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index eaa902a3..5b253511 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -138,6 +138,7 @@ enum { TRACECMD_OPTION_TIME_SHIFT, TRACECMD_OPTION_GUEST, TRACECMD_OPTION_TSC2NSEC, + TRACECMD_OPTION_MAX, }; enum { diff --git a/lib/trace-cmd/include/trace-cmd-local.h b/lib/trace-cmd/include/trace-cmd-local.h index d65ff599..d3760483 100644 --- a/lib/trace-cmd/include/trace-cmd-local.h +++ b/lib/trace-cmd/include/trace-cmd-local.h @@ -37,6 +37,11 @@ struct data_file_write { bool check_file_state(unsigned long file_version, int current_state, int new_state); bool check_out_state(struct tracecmd_output *handle, int new_state); +unsigned long long +out_write_section_header(struct tracecmd_output *handle, unsigned short header_id, + char *description, enum tracecmd_section_flags flags, bool option); +int out_update_section_header(struct tracecmd_output *handle, unsigned long long offset); + struct cpu_data_source { int fd; int size; diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index e8c788ad..a8c1305d 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -332,6 +332,74 @@ int tracecmd_ftrace_enable(int set) return ret; } +__hidden unsigned long long +out_write_section_header(struct tracecmd_output *handle, unsigned short header_id, + char *description, enum tracecmd_section_flags flags, bool option) +{ + tsize_t endian8; + tsize_t offset; + short endian2; + int size; + + if (header_id >= TRACECMD_OPTION_MAX) + return -1; + if (!HAS_SECTIONS(handle)) + return 0; + offset = do_lseek(handle, 0, SEEK_CUR); + if (option) { + endian8 = convert_endian_8(handle, offset); + if (!tracecmd_add_option(handle, header_id, 8, &endian8)) + return -1; + } + /* Section ID */ + endian2 = convert_endian_2(handle, header_id); + if (do_write_check(handle, &endian2, 2)) + return (off_t)-1; + + /* Section description */ + if (do_write_check(handle, description, strlen(description) + 1)) + return (off_t)-1; + /* Section flags */ + endian2 = convert_endian_2(handle, flags); + if (do_write_check(handle, &endian2, 2)) + return (off_t)-1; + + offset = do_lseek(handle, 0, SEEK_CUR); + size = 0; + /* Reserve for section size */ + if (do_write_check(handle, &size, 4)) + return (off_t)-1; + return offset; +} + +__hidden int out_update_section_header(struct tracecmd_output *handle, unsigned long long offset) +{ + unsigned long long current; + unsigned int endian4; + int size; + + if (!HAS_SECTIONS(handle) || offset == 0) + return 0; + + current = do_lseek(handle, 0, SEEK_CUR); + /* The real size is the difference between the saved offset and + * the current offset - 4 bytes, the reserved space for the section size. + */ + size = current - offset; + if (size < 4) + return -1; + size -= 4; + if (do_lseek(handle, offset, SEEK_SET) == (off_t)-1) + return -1; + + endian4 = convert_endian_4(handle, size); + if (do_write_check(handle, &endian4, 4)) + return -1; + if (do_lseek(handle, current, SEEK_SET) == (off_t)-1) + return -1; + return 0; +} + static int read_header_files(struct tracecmd_output *handle) { tsize_t size, check_size, endian8;