From patchwork Mon Sep 13 12:47:48 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: 12488879 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8D8CC4332F for ; Mon, 13 Sep 2021 12:48:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C24BD6101B for ; Mon, 13 Sep 2021 12:48:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232959AbhIMMuN (ORCPT ); Mon, 13 Sep 2021 08:50:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235622AbhIMMtP (ORCPT ); Mon, 13 Sep 2021 08:49:15 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84A18C061574 for ; Mon, 13 Sep 2021 05:47:59 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id g19-20020a1c9d13000000b003075062d4daso1357758wme.0 for ; Mon, 13 Sep 2021 05:47:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dWVND2TUoyNPoKgjxvxK2sC5GgTVyBk7UPC5DpBJaxE=; b=Ga8QOfaecMD3oE3GjigWPghxNPA1zSAwPTqO/NDI+GV/QIpgnsuZqj2W6yREleD5Zv jOkimYvgyB6gg21LCahglz3IAcyOuXMsgaiwYmM0/7m9BcsdGclN3/mx8DSts2n+uRFi idIxLfjhDlSOiu0gFOIy2g6vnSBke/1xuBAKPn6pcNqjjFv1nRpGkAiDKfDsPL+z5FEY Uyp083ZiwwNs9ayeGFwYvNlDpf5QnwRYD98tnonWkIr1+FnniMcSIa6IEJFHNS7dO3pJ +aRjeBci9KxCdGMTPWQdccgsV+sMl4iSYdELDvNKSYBlUsAVp6Wlyy87wVhhCJs3SVlM TZjA== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=dWVND2TUoyNPoKgjxvxK2sC5GgTVyBk7UPC5DpBJaxE=; b=2dA6BhpxpNmcZIA12mYDYc5swGC64KNiPB/pvERxt+6nLhD4++ZzyFC0CC1rT57xxx nc3oDs7o+y3wmmrtp23Wp0l4UZx73dHSdL3ohmVnrgOua0y/V5RL/VS8TspujVgD6eYN gDh/bm42Z1Ai3RZlukTxlXRmxurN6zdc3pb4toeJcnTq8ZXC8RPNFpYHnKh6sXZCUb7u tzV1EO3vBRA161X+C7eUrD/84BtQW8vqLrm4D8yMV7u4DtmB9yr3PuRvuWF33RVJ/YXf qlYKYpvp2scPBOwWVaTcBDHV9LTQbTJr/1xhgNrnvhfenUOLK84gnl1ANWuX2LXKRT22 eIaw== X-Gm-Message-State: AOAM531iXqnuyaWKbM9VxD7Jgr/eJzsEfy6qNevFlrOvgI/Rb+4kru5a k8l3EV5tX/unec7MDkX5aE0gdhfM0bY= X-Google-Smtp-Source: ABdhPJwmHozgmsTJpntEdkuR9pOhO58yALrmv2X/uO9tbpqz43MxnRn5RdXL9bwIfgrMcjHNrAqveA== X-Received: by 2002:a05:600c:4154:: with SMTP id h20mr3623274wmm.172.1631537278122; Mon, 13 Sep 2021 05:47:58 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm7718077wmj.0.2021.09.13.05.47.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:47:57 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 2/8] trace-cmd library: Handle version 7 files when copying headers between files Date: Mon, 13 Sep 2021 15:47:48 +0300 Message-Id: <20210913124754.3679916-3-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913124754.3679916-1-tz.stoyanov@gmail.com> References: <20210913124754.3679916-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org When copying header data between trace files, handle the file versions. Internal library functions for copying these headers are fixed to work with trace file version 7: copy_header_files copy_ftrace_files copy_event_files copy_proc_kallsyms copy_command_lines Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/include/trace-cmd-local.h | 3 + lib/trace-cmd/trace-input.c | 211 ++++++++++++++++++++---- lib/trace-cmd/trace-output.c | 10 ++ lib/trace-cmd/trace-util.c | 8 +- 4 files changed, 200 insertions(+), 32 deletions(-) diff --git a/lib/trace-cmd/include/trace-cmd-local.h b/lib/trace-cmd/include/trace-cmd-local.h index 18d2d05b..92bc288a 100644 --- a/lib/trace-cmd/include/trace-cmd-local.h +++ b/lib/trace-cmd/include/trace-cmd-local.h @@ -46,6 +46,9 @@ int out_uncompress_block(struct tracecmd_output *handle); int out_compression_start(struct tracecmd_output *handle, bool compress); int out_compression_end(struct tracecmd_output *handle, bool compress); void out_compression_reset(struct tracecmd_output *handle, bool compress); +bool out_check_compression(struct tracecmd_output *handle); + +void out_set_file_state(struct tracecmd_output *handle, int new_state); unsigned long long out_copy_fd_compress(struct tracecmd_output *handle, int fd, unsigned long long max, unsigned long long *write_size); diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 5cc881a8..e4fdc534 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -4443,164 +4443,313 @@ static int read_copy_data(struct tracecmd_input *in_handle, return -1; } + +static bool check_in_state(struct tracecmd_input *handle, int new_state) +{ + return check_file_state(handle->file_version, handle->file_state, new_state); +} + static int copy_header_files(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) { + bool compress = out_check_compression(out_handle); + struct file_section *sec; + unsigned long long offset; unsigned long long size; - if (in_handle->file_state != TRACECMD_FILE_HEADERS - 1) + if (!check_in_state(in_handle, TRACECMD_FILE_HEADERS) || + !check_out_state(out_handle, TRACECMD_FILE_HEADERS)) return -1; + sec = section_open(in_handle, TRACECMD_OPTION_HEADER_INFO); + if (!sec) + return -1; + offset = out_write_section_header(out_handle, TRACECMD_OPTION_HEADER_INFO, + "headers", TRACECMD_SEC_FL_COMPRESS, true); + out_compression_start(out_handle, compress); + /* "header_page" */ if (read_copy_data(in_handle, 12, out_handle) < 0) - return -1; + goto error; if (read_copy_size8(in_handle, out_handle, &size) < 0) - return -1; + goto error; if (read_copy_data(in_handle, size, out_handle) < 0) - return -1; + goto error; /* "header_event" */ if (read_copy_data(in_handle, 13, out_handle) < 0) - return -1; + goto error; if (read_copy_size8(in_handle, out_handle, &size) < 0) - return -1; + goto error; if (read_copy_data(in_handle, size, out_handle) < 0) - return -1; + goto error; in_handle->file_state = TRACECMD_FILE_HEADERS; + if (out_compression_end(out_handle, compress)) + goto error; + out_set_file_state(out_handle, in_handle->file_state); + section_close(in_handle, sec); + + if (out_update_section_header(out_handle, offset)) + goto error; return 0; +error: + out_compression_reset(out_handle, compress); + section_close(in_handle, sec); + return -1; } static int copy_ftrace_files(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) { + bool compress = out_check_compression(out_handle); + struct file_section *sec; + unsigned long long offset; unsigned long long size; unsigned int count; unsigned int i; - if (in_handle->file_state != TRACECMD_FILE_FTRACE_EVENTS - 1) + if (!check_in_state(in_handle, TRACECMD_FILE_FTRACE_EVENTS) || + !check_out_state(out_handle, TRACECMD_FILE_FTRACE_EVENTS)) return -1; - if (read_copy_size4(in_handle, out_handle, &count) < 0) + sec = section_open(in_handle, TRACECMD_OPTION_FTRACE_EVENTS); + if (!sec) return -1; + offset = out_write_section_header(out_handle, TRACECMD_OPTION_FTRACE_EVENTS, + "ftrace events", TRACECMD_SEC_FL_COMPRESS, true); + + out_compression_start(out_handle, compress); + + if (read_copy_size4(in_handle, out_handle, &count) < 0) + goto error; for (i = 0; i < count; i++) { if (read_copy_size8(in_handle, out_handle, &size) < 0) - return -1; + goto error; if (read_copy_data(in_handle, size, out_handle) < 0) - return -1; + goto error; } in_handle->file_state = TRACECMD_FILE_FTRACE_EVENTS; + if (out_compression_end(out_handle, compress)) + goto error; + out_set_file_state(out_handle, in_handle->file_state); + + section_close(in_handle, sec); + + if (out_update_section_header(out_handle, offset)) + goto error; return 0; +error: + out_compression_reset(out_handle, compress); + section_close(in_handle, sec); + return -1; } static int copy_event_files(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) { + bool compress = out_check_compression(out_handle); + struct file_section *sec; + unsigned long long offset; unsigned long long size; char *system; unsigned int systems; unsigned int count; unsigned int i,x; - if (in_handle->file_state != TRACECMD_FILE_ALL_EVENTS - 1) + if (!check_in_state(in_handle, TRACECMD_FILE_ALL_EVENTS) || + !check_out_state(out_handle, TRACECMD_FILE_ALL_EVENTS)) return -1; - if (read_copy_size4(in_handle, out_handle, &systems) < 0) + sec = section_open(in_handle, TRACECMD_OPTION_EVENT_FORMATS); + if (!sec) return -1; + offset = out_write_section_header(out_handle, TRACECMD_OPTION_EVENT_FORMATS, + "events format", TRACECMD_SEC_FL_COMPRESS, true); + + out_compression_start(out_handle, compress); + + if (read_copy_size4(in_handle, out_handle, &systems) < 0) + goto error; for (i = 0; i < systems; i++) { system = read_string(in_handle); if (!system) - return -1; + goto error; if (do_write_check(out_handle, system, strlen(system) + 1)) { free(system); - return -1; + goto error; } free(system); if (read_copy_size4(in_handle, out_handle, &count) < 0) - return -1; + goto error; for (x=0; x < count; x++) { if (read_copy_size8(in_handle, out_handle, &size) < 0) - return -1; + goto error; if (read_copy_data(in_handle, size, out_handle) < 0) - return -1; + goto error; } } in_handle->file_state = TRACECMD_FILE_ALL_EVENTS; + if (out_compression_end(out_handle, compress)) + goto error; + out_set_file_state(out_handle, in_handle->file_state); + + section_close(in_handle, sec); + + if (out_update_section_header(out_handle, offset)) + goto error; return 0; +error: + out_compression_reset(out_handle, compress); + section_close(in_handle, sec); + return -1; } static int copy_proc_kallsyms(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) { + bool compress = out_check_compression(out_handle); + struct file_section *sec; + unsigned long long offset; unsigned int size; - if (in_handle->file_state != TRACECMD_FILE_KALLSYMS - 1) + if (!check_in_state(in_handle, TRACECMD_FILE_KALLSYMS) || + !check_out_state(out_handle, TRACECMD_FILE_KALLSYMS)) return -1; - if (read_copy_size4(in_handle, out_handle, &size) < 0) + sec = section_open(in_handle, TRACECMD_OPTION_KALLSYMS); + if (!sec) return -1; + offset = out_write_section_header(out_handle, TRACECMD_OPTION_KALLSYMS, + "kallsyms", TRACECMD_SEC_FL_COMPRESS, true); + + out_compression_start(out_handle, compress); + if (read_copy_size4(in_handle, out_handle, &size) < 0) + goto error; if (!size) - return 0; /* OK? */ + goto out; /* OK? */ if (read_copy_data(in_handle, size, out_handle) < 0) - return -1; - + goto error; +out: in_handle->file_state = TRACECMD_FILE_KALLSYMS; + if (out_compression_end(out_handle, compress)) + goto error; + out_set_file_state(out_handle, in_handle->file_state); + + section_close(in_handle, sec); + + if (out_update_section_header(out_handle, offset)) + goto error; return 0; +error: + out_compression_reset(out_handle, compress); + section_close(in_handle, sec); + return -1; } static int copy_ftrace_printk(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) { + bool compress = out_check_compression(out_handle); + struct file_section *sec; + unsigned long long offset; unsigned int size; - if (in_handle->file_state != TRACECMD_FILE_PRINTK - 1) + if (!check_in_state(in_handle, TRACECMD_FILE_PRINTK) || + !check_out_state(out_handle, TRACECMD_FILE_PRINTK)) return -1; - if (read_copy_size4(in_handle, out_handle, &size) < 0) + sec = section_open(in_handle, TRACECMD_OPTION_PRINTK); + if (!sec) return -1; + offset = out_write_section_header(out_handle, TRACECMD_OPTION_PRINTK, + "printk", TRACECMD_SEC_FL_COMPRESS, true); + + out_compression_start(out_handle, compress); + + if (read_copy_size4(in_handle, out_handle, &size) < 0) + goto error; if (!size) - return 0; /* OK? */ + goto out; /* OK? */ if (read_copy_data(in_handle, size, out_handle) < 0) - return -1; + goto error; +out: in_handle->file_state = TRACECMD_FILE_PRINTK; + if (out_compression_end(out_handle, compress)) + goto error; + out_set_file_state(out_handle, in_handle->file_state); + + section_close(in_handle, sec); + + if (out_update_section_header(out_handle, offset)) + goto error; return 0; +error: + out_compression_reset(out_handle, compress); + section_close(in_handle, sec); + return -1; } static int copy_command_lines(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle) { + bool compress = out_check_compression(out_handle); + struct file_section *sec; + unsigned long long offset; unsigned long long size; - if (in_handle->file_state != TRACECMD_FILE_CMD_LINES - 1) + if (!check_in_state(in_handle, TRACECMD_FILE_CMD_LINES) || + !check_out_state(out_handle, TRACECMD_FILE_CMD_LINES)) return -1; - if (read_copy_size8(in_handle, out_handle, &size) < 0) + sec = section_open(in_handle, TRACECMD_OPTION_CMDLINES); + if (!sec) return -1; + offset = out_write_section_header(out_handle, TRACECMD_OPTION_CMDLINES, + "command lines", TRACECMD_SEC_FL_COMPRESS, true); + + out_compression_start(out_handle, compress); + + if (read_copy_size8(in_handle, out_handle, &size) < 0) + goto error; if (!size) - return 0; /* OK? */ + goto out; /* OK? */ if (read_copy_data(in_handle, size, out_handle) < 0) - return -1; + goto error; +out: in_handle->file_state = TRACECMD_FILE_CMD_LINES; + if (out_compression_end(out_handle, compress)) + goto error; + out_set_file_state(out_handle, in_handle->file_state); + + section_close(in_handle, sec); + + if (out_update_section_header(out_handle, offset)) + goto error; return 0; +error: + out_compression_reset(out_handle, compress); + section_close(in_handle, sec); + return -1; } /** diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index 2c55e95c..ea5407f1 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -2502,11 +2502,21 @@ out_free: return NULL; } +__hidden void out_set_file_state(struct tracecmd_output *handle, int new_state) +{ + handle->file_state = new_state; +} + __hidden bool check_out_state(struct tracecmd_output *handle, int new_state) { return check_file_state(handle->file_version, handle->file_state, new_state); } +__hidden bool out_check_compression(struct tracecmd_output *handle) +{ + return (handle->compress != NULL); +} + /** * tracecmd_get_out_file_version - return the trace.dat file version * @handle: output handle for the trace.dat file diff --git a/lib/trace-cmd/trace-util.c b/lib/trace-cmd/trace-util.c index 97db6ed9..e9e444cd 100644 --- a/lib/trace-cmd/trace-util.c +++ b/lib/trace-cmd/trace-util.c @@ -637,6 +637,12 @@ static void __attribute__((destructor)) tracecmd_lib_free(void) __hidden bool check_file_state(unsigned long file_version, int current_state, int new_state) { + if (file_version >= FILE_VERSION_SECTIONS) { + if (current_state < TRACECMD_FILE_INIT) + return false; + return true; + } + switch (new_state) { case TRACECMD_FILE_HEADERS: case TRACECMD_FILE_FTRACE_EVENTS: @@ -649,7 +655,7 @@ __hidden bool check_file_state(unsigned long file_version, int current_state, in return true; break; case TRACECMD_FILE_OPTIONS: - if (current_state == (new_state - 1)) + if (file_version < FILE_VERSION_SECTIONS && current_state == TRACECMD_FILE_CPU_COUNT) return true; break; case TRACECMD_FILE_CPU_LATENCY: