From patchwork Fri Sep 10 13:47:37 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: 12485145 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 0904FC433EF for ; Fri, 10 Sep 2021 13:48:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E1427611CB for ; Fri, 10 Sep 2021 13:48:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233683AbhIJNtW (ORCPT ); Fri, 10 Sep 2021 09:49:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233713AbhIJNtN (ORCPT ); Fri, 10 Sep 2021 09:49:13 -0400 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28E9DC061756 for ; Fri, 10 Sep 2021 06:48:02 -0700 (PDT) Received: by mail-ed1-x534.google.com with SMTP id 9so2564594edx.11 for ; Fri, 10 Sep 2021 06:48:02 -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=q/XpTU2XhoHbHAqoA8x/vtqptp62mgjiQRNtFM2Rvqo=; b=QlA5EgGykBdC0yuDHgoHIf6dfTaUmFgKl8EDdN+rtnLLyKlhD/yU0xhdCYDxpBCqXy 7MVflQ7TkbeMzEjWN3AWVKkooIiZHQCXdB4pRqF8qm933nWUqzIU+9kO/t1d5sclYESR ZqWYzsND7C0xRT4atdM1YSmNvhCk5w5LFq/Xw1NTN2k/4Bwymx9sMkuMF0cl3xYvNFSn vGxhM6Wtop5P+jtnRwTyw2PxD++sMXMpZe6CrWI08quWf+k0l+7eBjIBMW+haAHwFn7i uk/m6BWWs0V8B70LIOlwk0fcHgZYTD5e5MftXZchflgfH31ir1KM0k1CaEcg7WyVo9Q9 GEpg== 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=q/XpTU2XhoHbHAqoA8x/vtqptp62mgjiQRNtFM2Rvqo=; b=bty+FJkfcMGchK+Mw3hV5m41z67OH2lNb+WCspRsIw22ic1wdcUCwap0TYUYw3mK1C 6KAUgek+yfyZ4H0dQyhUV+BC8Xw/GjjcZf9VuLZnUe+qZzgQGNxuXZsDgXj3b5kFNphb lvO56pfJkLRSse436Ah2OSVTJzj0sK+Azuc85d1zx91Vj3C7uwj39NC0Fq3q8ts0DsAf jqi67MeKKoGKV9pB+Q/iOwHDULO2dTIcYezLI8lAacA/+UhFdxeAco9truAtns11DF2d 0HXLtevpYeaLypMC1lDByTUo0m2ytmTepnWoXqI3cGk0ZDRvvDvkKrEnwuD/LbhKPMWQ RgWg== X-Gm-Message-State: AOAM532eWzISibrSTRksKdzBTZrLDxbsHYMjCIwpYx62l6s1WqM/jVg2 u4p2jnQ3tsO70KeSRfnA67ZE82u057E= X-Google-Smtp-Source: ABdhPJwtVH2t7bjMYvuIG7nuc1tZVWXzNJFcvXXI406xDf4ndZCy5ILScNVx/wJ6UZGyTcTPt5qejQ== X-Received: by 2002:a05:6402:2695:: with SMTP id w21mr1244207edd.182.1631281680770; Fri, 10 Sep 2021 06:48:00 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.47.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:00 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 01/21] trace-cmd library: Read option id with correct endian Date: Fri, 10 Sep 2021 16:47:37 +0300 Message-Id: <20210910134757.2863982-2-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org The id of a trace option is 2 bytes short integer. When reading it from the trace file, use the tep handler associated with the file, if available, to convert the option with the correct endian order. A new helper function is introduced to read and convert 2 byte integer. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-input.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index ac57bc4f..0dbcdbdc 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -336,6 +336,18 @@ static char *read_string(struct tracecmd_input *handle) return NULL; } +static int read2(struct tracecmd_input *handle, unsigned short *size) +{ + struct tep_handle *pevent = handle->pevent; + unsigned short data; + + if (do_read_check(handle, &data, 2)) + return -1; + + *size = tep_read_number(pevent, &data, 2); + return 0; +} + static int read4(struct tracecmd_input *handle, unsigned int *size) { struct tep_handle *pevent = handle->pevent; @@ -2660,16 +2672,15 @@ static int handle_options(struct tracecmd_input *handle) handle->options_start = lseek64(handle->fd, 0, SEEK_CUR); for (;;) { - if (do_read_check(handle, &option, 2)) + if (read2(handle, &option)) return -1; if (option == TRACECMD_OPTION_DONE) break; /* next 4 bytes is the size of the option */ - if (do_read_check(handle, &size, 4)) + if (read4(handle, &size)) return -1; - size = tep_read_number(handle->pevent, &size, 4); buf = malloc(size); if (!buf) return -ENOMEM; From patchwork Fri Sep 10 13:47:38 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: 12485149 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 63D00C4332F for ; Fri, 10 Sep 2021 13:48:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4EED0611C9 for ; Fri, 10 Sep 2021 13:48:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233632AbhIJNtW (ORCPT ); Fri, 10 Sep 2021 09:49:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233715AbhIJNtO (ORCPT ); Fri, 10 Sep 2021 09:49:14 -0400 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 275D3C061760 for ; Fri, 10 Sep 2021 06:48:03 -0700 (PDT) Received: by mail-ej1-x636.google.com with SMTP id x11so4460485ejv.0 for ; Fri, 10 Sep 2021 06:48:03 -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=rHEDizwZjviRdumQYx/LHW1ieeaoKb8L1up3BfvtlPc=; b=krH9xYLpRPhC8RR/kAleVk3wlmz8bMAal1E10nfW/0SvPOlsg0uPC/F23A+4HDhLZN roF6d8ElD5tq4Sd2ln7jnHX3+glXK0kVMq/0qWlzviDLk/vz6E3hQg8MMcOLPkFJ2cDm m+km52yXrl6pMeuNhj1UCW5QMp2vwzDzMovzWJ7EvW2K0Ml68US955QsegkaAGCqryqi 6GDKMBcqSq39hLybedfqWYCqCU8RF+vgeJ4LxQCpTlovZFc7ZbWdJ+nsSod8Rk5dllNL V3OjVtE3VUza3Ejx1//FPT0rBbGJ6JUGPeYDIWjMCGZulEadD3bVvzGvFOhjVaMwcYt5 b2RA== 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=rHEDizwZjviRdumQYx/LHW1ieeaoKb8L1up3BfvtlPc=; b=zzAX1TIwA1WWl/+uIfuWUDJjmfnAckC6fDS2cvpY+hF2N2mZumUh6Ky3uczK/X16CR aLANraz+vaX9bC8cFL+vkyqPQ3mkT4Fym7mh4CEIUoU3t1B0PjdE34kEV511C2xC2sPp gpw3JvI2VKGa77j6w671J8OfQwMKPA1yFhopPynl126yvvrdMh87TDTseYcK82nu5dU2 x9QLaa/UWjqnGw7BVSRDKBvftDiqt71oKWoAC8d0X/8/Dc0XMHhM/LYEq+tTZHUXMCu7 PufPkAA7yhE8T+nI8ST/6muE3dqYBw/tX9Lm6lJy5i2tgeSDMXWhqLne/HNtf0o3Rpu0 R0bQ== X-Gm-Message-State: AOAM530n0bCRKy4iWi2Z2dr34nflx6vGhZM9bsFhcJU0btWJ6omiHNZf TmNvv7yhQbiklPzITnucNOgqnLakOQ0= X-Google-Smtp-Source: ABdhPJylGZMRI8wmo4liTwZPlOnrM4sfJAee0PMAeWDGaSz53LMgU+ZDeHMwgiPXxPnlIBmQ/2l6tQ== X-Received: by 2002:a17:906:3f95:: with SMTP id b21mr9170306ejj.368.1631281681799; Fri, 10 Sep 2021 06:48:01 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:01 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 02/21] trace-cmd report: Fix typos in error messages Date: Fri, 10 Sep 2021 16:47:38 +0300 Message-Id: <20210910134757.2863982-3-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Fixed typos in "trace-cmd report" messages that report reading and parsing errors. Signed-off-by: Tzvetomir Stoyanov (VMware) --- tracecmd/trace-read.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tracecmd/trace-read.c b/tracecmd/trace-read.c index 6f43c1d2..31724b09 100644 --- a/tracecmd/trace-read.c +++ b/tracecmd/trace-read.c @@ -1290,7 +1290,7 @@ static void read_data_info(struct list_head *handle_list, enum output_type otype die("error in reading buffer instance"); new_handle = tracecmd_buffer_instance_handle(handles->handle, i); if (!new_handle) { - warning("could not retreive handle %s", name); + warning("could not retrieve handle %s", name); continue; } add_handle(new_handle, name); @@ -1324,7 +1324,7 @@ static void read_data_info(struct list_head *handle_list, enum output_type otype if (last_record) { int cpu = last_record->cpu; if (cpu >= last_handle->cpus) - die("cpu %d creater than %d\n", cpu, last_handle->cpus); + die("cpu %d greater than %d\n", cpu, last_handle->cpus); if (tscheck && last_handle->last_timestamp[cpu] > last_record->ts) { errno = 0; From patchwork Fri Sep 10 13:47:39 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: 12485153 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 B9038C433FE for ; Fri, 10 Sep 2021 13:48:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A2420611C9 for ; Fri, 10 Sep 2021 13:48:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233707AbhIJNtW (ORCPT ); Fri, 10 Sep 2021 09:49:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233730AbhIJNtP (ORCPT ); Fri, 10 Sep 2021 09:49:15 -0400 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23021C061762 for ; Fri, 10 Sep 2021 06:48:04 -0700 (PDT) Received: by mail-ej1-x62c.google.com with SMTP id h9so4411105ejs.4 for ; Fri, 10 Sep 2021 06:48:04 -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=t6f35bOqeR513Bi7TdiB4HXAWp6UmqxXje43WtBoeKo=; b=If1AwQprV5ZGgvFj4HkgTHldkuYoTQU65DI9E3JbASmpA/1M6NXBAJKNRFGO39slfw kxBRu+qTrqmLzVBkMINzznIubbzRrHhmU/DVbtOpF0KCzx8SAlpSTvs+6rCoiKdka5Jc AQA2ioMFeGrrbIuFclZ+7r8EY8xffc2+b2n15UNyC7S4yevgfLoP+tPEmmHCMG1KGORL JBL4yUHOGHdzlR74lKbs7bAhgGscaN0bBcy++1U63WPX51j/HwnTmoED6POWtz3wT4bC EkJqG8cw6Srewcz816VqnZD/zyhlboHF7eddYJYTDrnBBSV+gDqa7rHlTuA9YS+RFg8F i8OQ== 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=t6f35bOqeR513Bi7TdiB4HXAWp6UmqxXje43WtBoeKo=; b=VBcVtbqlh7dYbExiy7PDsajk4KDWPmkBdlQsufBdcTgd14KqPSKQRd0P5TUSw9duM6 IUf7IaeLywOY7XJiPv74aqpoTsBfsCe3/k/3hN88wZeRdtMRpMzQCmfpIiw8NlumePFk dAmyGEpn4RmTSeUdDrrkFO7Le1VsyB0pdHI1cciG2qOY6m7TCELkWR0BkkLZy0xM5rwS PGAMJVZprikJrkthYVlY1GRDiNzd+CMhfF8BLnnriR5MS2nM6nhHJNLC5G+BDnCbb2OD tdblfyd+JxB+hpLJwNYywNGuDY2w4tys7KhHaPLm/givoIBPk9G2m7mD3y3Z3it9fm4h iA2A== X-Gm-Message-State: AOAM532Tg2Riru5nGJ1YW+PGN5Wwonbtz0HcMlXiSN7lThWM9NDiyb+g G6Z8Zp4Kw5yV6SzgEeY6zIJ6ggPGBtI= X-Google-Smtp-Source: ABdhPJztOgNWlQW5T8HKJz0ypmVqDWRkkWSLhJ0S5HFVK1MXstF9XPX6jc2h9tojG6Il314xqaqoBA== X-Received: by 2002:a17:906:48d0:: with SMTP id d16mr9225279ejt.562.1631281682759; Fri, 10 Sep 2021 06:48:02 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:02 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 03/21] trace-cmd library: Fix version string memory leak Date: Fri, 10 Sep 2021 16:47:39 +0300 Message-Id: <20210910134757.2863982-4-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org The version string is allocated when a VERSION option is processed, but is never freed. Free it on input handler close. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-input.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 0dbcdbdc..9253bc37 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -3518,6 +3518,7 @@ void tracecmd_close(struct tracecmd_input *handle) free(handle->cpu_data); free(handle->uname); free(handle->trace_clock); + free(handle->version); close(handle->fd); tracecmd_free_hooks(handle->hooks); @@ -3959,6 +3960,7 @@ tracecmd_buffer_instance_handle(struct tracecmd_input *handle, int indx) new_handle->cpu_data = NULL; new_handle->nr_buffers = 0; new_handle->buffers = NULL; + new_handle->version = NULL; new_handle->ref = 1; if (handle->trace_clock) { new_handle->trace_clock = strdup(handle->trace_clock); From patchwork Fri Sep 10 13:47:40 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: 12485187 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 AE695C433F5 for ; Fri, 10 Sep 2021 13:49:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8D5EE611C9 for ; Fri, 10 Sep 2021 13:49:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233485AbhIJNuN (ORCPT ); Fri, 10 Sep 2021 09:50:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233245AbhIJNtQ (ORCPT ); Fri, 10 Sep 2021 09:49:16 -0400 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 3EF33C061766 for ; Fri, 10 Sep 2021 06:48:05 -0700 (PDT) Received: by mail-ed1-x52c.google.com with SMTP id t6so1156833edi.9 for ; Fri, 10 Sep 2021 06:48:05 -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=6uflLdcxtwKc9UdYaXGxzpx3D+nzjM4vg/8RpU5kByg=; b=gQnB/3rF6Kj6cAutgS0u47p9WPvEps5pTd18FQo7YgkhypLbSqVVbwM7dHwV8eOCWW L3F6d7/Qh83y1aU5Bs8hZNmJOEcin21AMQ56mCEqEqR8QJ2hIF88APbTWmMoJshCUeGy 5kznTMr7IkY5FB9T8KHM0zqFouA62xl4caPYK/66CpgX/X2JOIZhHNvWc82+KM+kN9IU M/ZkarX4n7y56qlvsi+9xXLPwXwSQ3An116O74i50V8Za1t6+yhzM/8wTfHNIgMqiVwX Pzj0QWFcdvBsUKPHpCioT4hjOYaYxJ6D8zPy3mX+OZ1OGoSQ2eopqkBM8jFLpe8AMXwW Xzig== 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=6uflLdcxtwKc9UdYaXGxzpx3D+nzjM4vg/8RpU5kByg=; b=u3b61LmZSnMs+hVeScJ7P+L6whJ8bMqDoVLHbuYoFXxWOKNiJ3jrmLWFwZiER/fQVT Nf5C7H1jHPdP1OiB6iarrPULRB+TortwmjMXyZWAzqH5+vaohD8sxJLAf0jWNoQlOc91 qdIzooIuwfiUj1Ce190ONvsZuKJO26l0Yr74+I8iCAle3TpW4t40pSJuMqQpEb6km5aA yQz3LmgOWZ4SqxgPr25BeRjOabGKmc/bG3x4B6Mf/DTk86Dr/GrNR0vb++11S9WXjzOO u1s5+pTCqXMdnoqXtnsJkoR/OT6SQc0rfXpq2tdUvsXmPgztvrIy+1OkX+TOWD5FuYjr k/gw== X-Gm-Message-State: AOAM531NRWtWs1cAxwaRV7LWDL3Eduxx3/oqI+vvT1kCnhTSsYMX4ANw vFWAQQ2h2dN1S9BVje116EWF5ck6vng= X-Google-Smtp-Source: ABdhPJwXTba8+hW8LEEVyvU/Sc48regOJykdyu4cX9uok63RYwlj1qaJXy0tGSvoHntAoQPNRcbamA== X-Received: by 2002:a05:6402:5107:: with SMTP id m7mr9095359edd.63.1631281683809; Fri, 10 Sep 2021 06:48:03 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:03 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 04/21] trace-cmd library: Fixed a memory leak on input handler close Date: Fri, 10 Sep 2021 16:47:40 +0300 Message-Id: <20210910134757.2863982-5-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org When an input handler to a trace file is closed with tracecmd_close(), the list with buffers is not freed. This leads to a memory leak. Added logic to free that list. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-input.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 9253bc37..ffe87e8a 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -3484,6 +3484,7 @@ void tracecmd_ref(struct tracecmd_input *handle) void tracecmd_close(struct tracecmd_input *handle) { int cpu; + int i; if (!handle) return; @@ -3521,6 +3522,10 @@ void tracecmd_close(struct tracecmd_input *handle) free(handle->version); close(handle->fd); + for (i = 0; i < handle->nr_buffers; i++) + free(handle->buffers[i].name); + free(handle->buffers); + tracecmd_free_hooks(handle->hooks); handle->hooks = NULL; From patchwork Fri Sep 10 13:47:41 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: 12485151 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 4D916C43219 for ; Fri, 10 Sep 2021 13:48:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 350F1611C9 for ; Fri, 10 Sep 2021 13:48:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233602AbhIJNtX (ORCPT ); Fri, 10 Sep 2021 09:49:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233767AbhIJNtR (ORCPT ); Fri, 10 Sep 2021 09:49:17 -0400 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3811AC061574 for ; Fri, 10 Sep 2021 06:48:06 -0700 (PDT) Received: by mail-ed1-x529.google.com with SMTP id eb14so2567479edb.8 for ; Fri, 10 Sep 2021 06:48:06 -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=ZeyT15SqFOCewNiJ5wWZIoGsE6RejnJ66ku3xcCgS5c=; b=LaYG8DjKF5Zm+ovhEGKCkZUt2fDVpIpDY6/YLWgrJiuOc8GSwBi5S4of4TQ/zwrrER VkDk6G7Vy2mUwUYK+pGdQTU7yHV2b0R1oZaORpJlZjPKq1AoNhilvml0mD2pePch0/XZ pLF8DYGZCpvtz+ffhjM62qMnT3NgVzPmHps5vTvjYVf83bpMFIQ7JpyjHdBVHS/E6kmw araWoNMZu9xMd4++33SsQv95e9XRNYeHs44ux/rqHk7Gd/xwVWqu68PR5CbUaeYwhMDw 4ay+w6f1554DXf1MGUWpxHk8sTbdjrLeUBW9ep6AE9Qzpn18B4aMkLOYZSTa/rWtBtY0 sVzQ== 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=ZeyT15SqFOCewNiJ5wWZIoGsE6RejnJ66ku3xcCgS5c=; b=Xg/rhx5dO0AvkFThoxfQd7z3kiFi3efwgQ20PUDThOCy5wKIQgwMXzHupJ6lsxQNPX yf609tYK78Z6AsLNJyq5ZOss4f+cw5zZupNBjVCMQqD+LxwYvqX6+AhyQ8+eCffYzYeh F0a/Ay2MhQuluzqGFooLsYPPKldhmagxoyWHQr28uc7ZYHz2yJsjfYuIgLNih5HJ0/At 5DtLNsq4rRUgqFnVqswMetaHhrnss7Plegr6R5JSma+B7cSu15sMvqyTa8yzbTuuKG0d uf62TtHFWw/ypedWAkLNYohr9ivlcF2SL6Utdrjh8cn1rfiC5aAcpDPkyQjYoe3jSAXl 94tg== X-Gm-Message-State: AOAM532H8tRVPGBgiljTfPBfblxovHCmrmrBVX8M2+JOVC+CGm2EWmiv /Re22rr9l9tNmb4dQ3qcC+6pnqlehp8= X-Google-Smtp-Source: ABdhPJxEw/GuFA2xvNS//BTHte1OwYRvFUZ5xMu8K0jnT8uL36u8HKJsephzmBYliBpXyXww1vEe5g== X-Received: by 2002:aa7:c952:: with SMTP id h18mr9222522edt.18.1631281684827; Fri, 10 Sep 2021 06:48:04 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:04 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 05/21] trace-cmd library: Do not pass guests list to a buffer instance Date: Fri, 10 Sep 2021 16:47:41 +0300 Message-Id: <20210910134757.2863982-6-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org When a new trace buffer is read from the trace file, a new input handler is duplicated from the top one. The pointer to the guests list should not be duplicated, as it could lead to a memory corruption on handler close. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-input.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index ffe87e8a..e5c8c332 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -3966,6 +3966,7 @@ tracecmd_buffer_instance_handle(struct tracecmd_input *handle, int indx) new_handle->nr_buffers = 0; new_handle->buffers = NULL; new_handle->version = NULL; + new_handle->guest = NULL; new_handle->ref = 1; if (handle->trace_clock) { new_handle->trace_clock = strdup(handle->trace_clock); From patchwork Fri Sep 10 13:47:42 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: 12485155 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 AC225C433F5 for ; Fri, 10 Sep 2021 13:48:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 96B5A611C9 for ; Fri, 10 Sep 2021 13:48:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233710AbhIJNtX (ORCPT ); Fri, 10 Sep 2021 09:49:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233784AbhIJNtS (ORCPT ); Fri, 10 Sep 2021 09:49:18 -0400 Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7201FC061756 for ; Fri, 10 Sep 2021 06:48:07 -0700 (PDT) Received: by mail-ed1-x531.google.com with SMTP id n10so2573523eda.10 for ; Fri, 10 Sep 2021 06:48:07 -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=4eNSf3fXIhuexSWrZzu9VRBujGIACDgGvnuES2rhjo4=; b=YYYabyGYaNU44M32Jyj/uUZ+XA3KJnnqGfoQ0iSTqbxfYgRn0Tmoywhw53G7DqGlST /xcyNHph4WsWB9ZzdxiFn3VhbNTSYQhZ977yaG07S2uEYNHwd7fvGMbW8clV1DZsbZ08 HDT2+j415zZHKB7A0F+oBLLfeelLY1BY+SsIZMaFRbsd/2t3baGg5HJysIAfs+rU+gWT 0sikvOGNvYLTWH/3+R+Ba5oLoluLJK8JcUD7d4Zap3x7mgqI91qIVWXMlbLXTCaMRXml jfm5BhratBaI8hhjf0DNlk3EY/s+ihtfWRkGc4we4QtbqQXjo+HqbWPhpDhPTLk/j5rP 6Smw== 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=4eNSf3fXIhuexSWrZzu9VRBujGIACDgGvnuES2rhjo4=; b=VwCyO6cEkDzGMQ45f3KfHNYy9BTFCvq71rbB7+ul6ew16xA7/Zq7Ia3pJoDk6jH/ef xQqIplicw74ioHOY17z9w2YHWkFFqHSnKKxnbUyXh/p+tpZEC2C/El4lc6/0yM6Ygz3b 6KSthcczVMd91Uv1SnJS1J7yUXqks8tbLFfnjxPx0hi3l5iDxi2MncAMkoLU0Fa+caR4 TOuCvbRILP0/SGaj3SY3HcQIdv9XOAjxRAObVHJ/xukaYQjkGRZW8gs/k7QTKu99eiQ9 C7dJqhrgN6MHP2sPA6w7qnFH7aK7g95J3ymvj8u5jLWIwdpP8ExYbtCFUA76c90KDyCM ki1w== X-Gm-Message-State: AOAM533UtjLm6/uCj4wRGjBfp4QPbfjKYVlcEfSW3woN9YRkj8JrAhIT Q4yvecVqzzxS4FMwj8/hCFsxdJZiU68= X-Google-Smtp-Source: ABdhPJyevycU3T7Z+EyWZGjex+OQ2qXpDwXhrRzJGDlaJctRHuAsgpTeCCGLP++yYBiqL96mXgL+IQ== X-Received: by 2002:aa7:d710:: with SMTP id t16mr8941099edq.42.1631281685997; Fri, 10 Sep 2021 06:48:05 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:05 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 06/21] trace-cmd library: Set long size to the input tep handler when it is read from the file Date: Fri, 10 Sep 2021 16:47:42 +0300 Message-Id: <20210910134757.2863982-7-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Setting the long size to the input tep handler in tracecmd_read_headers() API may be too late, as this tep handler is used to read and parse data from the file before that. The most suitable place for that is tracecmd_alloc_fd() API, right after reading the long size from the file. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-input.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index e5c8c332..3d7f1c48 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -860,8 +860,6 @@ int tracecmd_read_headers(struct tracecmd_input *handle, if (ret < 0) return -1; - tep_set_long_size(handle->pevent, handle->long_size); - if (state <= handle->file_state) return 0; @@ -3337,6 +3335,7 @@ struct tracecmd_input *tracecmd_alloc_fd(int fd, int flags) do_read_check(handle, buf, 1); handle->long_size = buf[0]; + tep_set_long_size(handle->pevent, handle->long_size); read4(handle, &page_size); handle->page_size = page_size; From patchwork Fri Sep 10 13:47:43 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: 12485157 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 30DC2C433EF for ; Fri, 10 Sep 2021 13:48:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1A5CC611EF for ; Fri, 10 Sep 2021 13:48:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233713AbhIJNtY (ORCPT ); Fri, 10 Sep 2021 09:49:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233789AbhIJNtT (ORCPT ); Fri, 10 Sep 2021 09:49:19 -0400 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F8EDC061574 for ; Fri, 10 Sep 2021 06:48:08 -0700 (PDT) Received: by mail-ed1-x52f.google.com with SMTP id g8so2587237edt.7 for ; Fri, 10 Sep 2021 06:48:08 -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=X2Kqqc0Y/KLYPqbk6FuL8+pOWJtHC4qePmje4ffdblI=; b=iNHOSwim4WYgybGVmDk6w3S+xlPT+eZj2aHLz0CXa0SzuMhhEYJBaFl21q094mNSHS YAqlveBsNRuRlMBsWSSg0mjzAg23jeZLmQCkqwM8xT/QpJZjXKCBh56Mi47VcxoF0oji F6rpjp97I+AZXCIGetPdqK7LKIGepeQYgZdxr+zpbgiULlBkRqLOrOwAuj7MSBq3HN0v scrhtj2eNImMuosqOMEc0m2Z0pGbBpTYqxsTCJeHWC+rS2Snga0Q2IZnG20IBLQa/KzX 1V94ZqKEZKSygO+1w9RGYKW1PAFlq3P2ZyVPqQ42M3J5h/F3oNZ4DkuZldtqavyMfPa9 kXgA== 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=X2Kqqc0Y/KLYPqbk6FuL8+pOWJtHC4qePmje4ffdblI=; b=7kRKBLXFbM/u8tX9LElQgq6zA+qQ8IYVmm1nR7RwD9L3Za+tSQfLBTXQLapKeBfo2v P5D9cTnwm47XV2QMu8wV416fj4rgFFpGMZK7mTpvGHDTTUMsi8wkRw5mGdTLxmNoSukH v/odsWAB/BSK6mddeM+e3+eWWL2s50xtCLUWzom1op/xg59nbjP2WO4QeFfKVkR33/3c 6GdiNynAj2sdlkwlYtU/QjsRSMrU0Ek/mKnX91VtSrVpLG2/TsOC2yPmC4iaSVIqcqFY 54dSnmDuW8NSRrB9kh8qJ0f5DiEGkBKbtIBcensTY4AUYqK+/82zrbbm8fq98EjnCMd+ F/QA== X-Gm-Message-State: AOAM530ZbyFKHLqvJg3qIWDXT5QFBlS06A3gXQpCmVeMvNl8r894uvcm BsFlrd6VggnMOnQuEQxNWcvC0ZjXDew= X-Google-Smtp-Source: ABdhPJxXBdJGKGG50iP93/cjFGcuXcp3NrbTn6qdrb5cXHxGvm+tLf/jDVBaPzRRf2otbMXXhR3urQ== X-Received: by 2002:a50:d713:: with SMTP id t19mr9087842edi.2.1631281687031; Fri, 10 Sep 2021 06:48:07 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:06 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 07/21] trace-cmd library: Do not use local variables when reading CPU stat option Date: Fri, 10 Sep 2021 16:47:43 +0300 Message-Id: <20210910134757.2863982-8-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Using a local variable to read all CPUSTAT options assumes that all of them are in a single option section. While this is true for the current trace file version format, this assumption limits the design of a more flexible format with multiple options sections. Use input handler context instead of the local variable. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-input.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 3d7f1c48..dd7689a3 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -138,6 +138,7 @@ struct tracecmd_input { struct host_trace_info host; double ts2secs; + unsigned int cpustats_size; char * cpustats; char * uname; char * version; @@ -2658,7 +2659,6 @@ static int handle_options(struct tracecmd_input *handle) unsigned short option; unsigned int size; char *cpustats = NULL; - unsigned int cpustats_size = 0; struct input_buffer_instance *buffer; struct hook_list *hook; char *buf; @@ -2738,12 +2738,16 @@ static int handle_options(struct tracecmd_input *handle) break; case TRACECMD_OPTION_CPUSTAT: buf[size-1] = '\n'; - cpustats = realloc(cpustats, cpustats_size + size + 1); - if (!cpustats) - return -ENOMEM; - memcpy(cpustats + cpustats_size, buf, size); - cpustats_size += size; - cpustats[cpustats_size] = 0; + cpustats = realloc(handle->cpustats, + handle->cpustats_size + size + 1); + if (!cpustats) { + ret = -ENOMEM; + return ret; + } + memcpy(cpustats + handle->cpustats_size, buf, size); + handle->cpustats_size += size; + cpustats[handle->cpustats_size] = 0; + handle->cpustats = cpustats; break; case TRACECMD_OPTION_BUFFER: /* A buffer instance is saved at the end of the file */ From patchwork Fri Sep 10 13:47:44 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: 12485161 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 7EF2EC4321E for ; Fri, 10 Sep 2021 13:48:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 68777611C9 for ; Fri, 10 Sep 2021 13:48:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233711AbhIJNtY (ORCPT ); Fri, 10 Sep 2021 09:49:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47952 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233806AbhIJNtU (ORCPT ); Fri, 10 Sep 2021 09:49:20 -0400 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6DC3CC061574 for ; Fri, 10 Sep 2021 06:48:09 -0700 (PDT) Received: by mail-ed1-x529.google.com with SMTP id g21so2586073edw.4 for ; Fri, 10 Sep 2021 06:48:09 -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=oTzbxfgmdv5dS26KmRX3/wLchys/YsUZn4fQBqrOuTQ=; b=GNEMPnysRBHbovaEb4g4Cr86ZOjdqgphNO+nUjkCjgMTmEcz8EjnlbFZgJ4zIq8yWf Xk77cCD+jUo0t1+ljSSk18ylmtlQW2HbV81YHw9MLOXR2l4yEfhJ1yckpKM5jCyXRE4p BvqwrdIQQkw9w9MwEz5S6IClgAjJnuwYhIUAIM/cNsVAyqWAxB52C8sPqv7d58GXENrg z4h6E9Dq6/jzDLaa9be0NG9xSJgiCuNIQ6WTayE5F8yskXR9nUyirIh/UuCa/74HHBvc Pgx2WzVtnsQCreeh5CnfHvE9G40u7p7KbTZnkVKv6IvdsJha58OUznptCN945/4ipZGv Xgfg== 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=oTzbxfgmdv5dS26KmRX3/wLchys/YsUZn4fQBqrOuTQ=; b=KlKAk2W9suG+c4lrseEsFx5kVKAtfbXSallaDBoSjRTccaLyXpRfr3HLow3dYxTuEe QUCF7ds1AU2vNzRO7SngjIxSZWSQLG38219vKay3+b2kDLDrFeb/L1TA91I9Qglkr1nW hPZtu5nhSl2TnAIVV7rOAn/FMmaMMNOAuGZCyVLWtXcpMJ9qGyq+w2ujCjW50hx5ifyv 2jGXhInEM1se2tQB1RNg3Mpjt+vY2dLQp9y2ykyS3WvNV5HwDt1f7C/fKpIZXJWFOy1K rYsYUzaCmxaEuAuWd7jJmBfmDobPSSU3jnIiOJyXzVQazFQWGx91nsQoZfmN/mPWUjon NZvA== X-Gm-Message-State: AOAM532qUsXoUXJe5hd4ov61hyYpdr++arNWbG1axk6oIDGDPwla2mM+ ByDLmCUwO9g0rYwQ6F0zss6zUwbZ22g= X-Google-Smtp-Source: ABdhPJz98akedkDteTg3xvP27WFpwWJ1v5MZLsfhA74SMdtLQ+/J1ucxBYh+NHMTeGi0+O7sAX5+AA== X-Received: by 2002:a05:6402:524e:: with SMTP id t14mr9125757edd.66.1631281688031; Fri, 10 Sep 2021 06:48:08 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:07 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 08/21] trace-cmd library: Reuse within the library the function that checks file state. Date: Fri, 10 Sep 2021 16:47:44 +0300 Message-Id: <20210910134757.2863982-9-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Make the function, that checks if the next file state is valid, global for the trace-cmd library, so it can be reused. It is important the same check logic to be used in the whole library. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/include/trace-cmd-local.h | 2 + lib/trace-cmd/trace-input.c | 1 + lib/trace-cmd/trace-output.c | 66 ++++++++----------------- lib/trace-cmd/trace-util.c | 28 +++++++++++ 4 files changed, 51 insertions(+), 46 deletions(-) diff --git a/lib/trace-cmd/include/trace-cmd-local.h b/lib/trace-cmd/include/trace-cmd-local.h index 821b5cdb..7f3b45a8 100644 --- a/lib/trace-cmd/include/trace-cmd-local.h +++ b/lib/trace-cmd/include/trace-cmd-local.h @@ -31,5 +31,7 @@ void tracecmd_info(const char *fmt, ...); #endif #endif +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); #endif /* _TRACE_CMD_LOCAL_H */ diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index dd7689a3..2f749d0b 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -4224,3 +4224,4 @@ int tracecmd_enable_tsync(struct tracecmd_input *handle, bool enable) return 0; } + diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index a8de107c..fe1d1aaa 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -307,33 +307,6 @@ int tracecmd_ftrace_enable(int set) return ret; } -static int check_out_state(struct tracecmd_output *handle, int new_state) -{ - if (!handle) - return -1; - - switch (new_state) { - case TRACECMD_FILE_HEADERS: - case TRACECMD_FILE_FTRACE_EVENTS: - case TRACECMD_FILE_ALL_EVENTS: - case TRACECMD_FILE_KALLSYMS: - case TRACECMD_FILE_PRINTK: - case TRACECMD_FILE_CMD_LINES: - case TRACECMD_FILE_CPU_COUNT: - case TRACECMD_FILE_OPTIONS: - if (handle->file_state == (new_state - 1)) - return 0; - break; - case TRACECMD_FILE_CPU_LATENCY: - case TRACECMD_FILE_CPU_FLYRECORD: - if (handle->file_state == TRACECMD_FILE_OPTIONS) - return 0; - break; - } - - return -1; -} - static int read_header_files(struct tracecmd_output *handle) { tsize_t size, check_size, endian8; @@ -342,7 +315,7 @@ static int read_header_files(struct tracecmd_output *handle) int fd; int ret; - if (check_out_state(handle, TRACECMD_FILE_HEADERS) < 0) { + if (!check_out_state(handle, TRACECMD_FILE_HEADERS)) { tracecmd_warning("Cannot read header files, unexpected state 0x%X", handle->file_state); return -1; @@ -654,7 +627,7 @@ static int read_ftrace_files(struct tracecmd_output *handle) struct tracecmd_event_list list = { .glob = "ftrace/*" }; int ret; - if (check_out_state(handle, TRACECMD_FILE_FTRACE_EVENTS) < 0) { + if (!check_out_state(handle, TRACECMD_FILE_FTRACE_EVENTS)) { tracecmd_warning("Cannot read ftrace files, unexpected state 0x%X", handle->file_state); return -1; @@ -695,7 +668,7 @@ static int read_event_files(struct tracecmd_output *handle, int endian4; int ret; - if (check_out_state(handle, TRACECMD_FILE_ALL_EVENTS) < 0) { + if (!check_out_state(handle, TRACECMD_FILE_ALL_EVENTS)) { tracecmd_warning("Cannot read event files, unexpected state 0x%X", handle->file_state); return -1; @@ -787,7 +760,7 @@ static int read_proc_kallsyms(struct tracecmd_output *handle, struct stat st; int ret; - if (check_out_state(handle, TRACECMD_FILE_KALLSYMS) < 0) { + if (!check_out_state(handle, TRACECMD_FILE_KALLSYMS)) { tracecmd_warning("Cannot read kallsyms, unexpected state 0x%X", handle->file_state); return -1; @@ -832,7 +805,7 @@ static int read_ftrace_printk(struct tracecmd_output *handle) char *path; int ret; - if (check_out_state(handle, TRACECMD_FILE_PRINTK) < 0) { + if (!check_out_state(handle, TRACECMD_FILE_PRINTK)) { tracecmd_warning("Cannot read printk, unexpected state 0x%X", handle->file_state); return -1; @@ -1134,11 +1107,10 @@ int tracecmd_write_cpus(struct tracecmd_output *handle, int cpus) { int ret; - ret = check_out_state(handle, TRACECMD_FILE_CPU_COUNT); - if (ret < 0) { + if (!check_out_state(handle, TRACECMD_FILE_CPU_COUNT)) { tracecmd_warning("Cannot write CPU count into the file, unexpected state 0x%X", handle->file_state); - return ret; + return -1; } cpus = convert_endian_4(handle, cpus); ret = do_write_check(handle, &cpus, 4); @@ -1154,16 +1126,14 @@ int tracecmd_write_options(struct tracecmd_output *handle) unsigned short option; unsigned short endian2; unsigned int endian4; - int ret; /* If already written, ignore */ if (handle->file_state == TRACECMD_FILE_OPTIONS) return 0; - ret = check_out_state(handle, TRACECMD_FILE_OPTIONS); - if (ret < 0) { + if (!check_out_state(handle, TRACECMD_FILE_OPTIONS)) { tracecmd_warning("Cannot write options into the file, unexpected state 0x%X", handle->file_state); - return ret; + return -1; } if (do_write_check(handle, "options ", 10)) @@ -1281,11 +1251,10 @@ int tracecmd_write_cmdlines(struct tracecmd_output *handle) { int ret; - ret = check_out_state(handle, TRACECMD_FILE_CMD_LINES); - if (ret < 0) { + if (!check_out_state(handle, TRACECMD_FILE_CMD_LINES)) { tracecmd_warning("Cannot write command lines into the file, unexpected state 0x%X", handle->file_state); - return ret; + return -1; } ret = save_tracing_file_data(handle, "saved_cmdlines"); if (ret < 0) @@ -1298,7 +1267,6 @@ struct tracecmd_output *tracecmd_create_file_latency(const char *output_file, in { struct tracecmd_output *handle; char *path; - int ret; handle = create_file(output_file, NULL, NULL, NULL, &all_event_list); if (!handle) @@ -1316,8 +1284,7 @@ struct tracecmd_output *tracecmd_create_file_latency(const char *output_file, in if (tracecmd_write_options(handle) < 0) goto out_free; - ret = check_out_state(handle, TRACECMD_FILE_CPU_LATENCY); - if (ret < 0) { + if (!check_out_state(handle, TRACECMD_FILE_CPU_LATENCY)) { tracecmd_warning("Cannot write latency data into the file, unexpected state 0x%X", handle->file_state); goto out_free; @@ -1399,7 +1366,9 @@ int tracecmd_write_cpu_data(struct tracecmd_output *handle, /* This can be called multiple times (when recording instances) */ ret = handle->file_state == TRACECMD_FILE_CPU_FLYRECORD ? 0 : - check_out_state(handle, TRACECMD_FILE_CPU_FLYRECORD); + check_file_state(handle->file_version, + handle->file_state, + TRACECMD_FILE_CPU_FLYRECORD); if (ret < 0) { tracecmd_warning("Cannot write trace data into the file, unexpected state 0x%X", handle->file_state); @@ -1661,3 +1630,8 @@ out_free: tracecmd_output_close(handle); return NULL; } + +__hidden bool check_out_state(struct tracecmd_output *handle, int new_state) +{ + return check_file_state(handle->file_version, handle->file_state, new_state); +} diff --git a/lib/trace-cmd/trace-util.c b/lib/trace-cmd/trace-util.c index 9bc94822..db7ce7ee 100644 --- a/lib/trace-cmd/trace-util.c +++ b/lib/trace-cmd/trace-util.c @@ -624,3 +624,31 @@ bool tracecmd_is_version_supported(unsigned int version) return true; return false; } + +__hidden bool check_file_state(unsigned long file_version, int current_state, int new_state) +{ + switch (new_state) { + case TRACECMD_FILE_HEADERS: + case TRACECMD_FILE_FTRACE_EVENTS: + case TRACECMD_FILE_ALL_EVENTS: + case TRACECMD_FILE_KALLSYMS: + case TRACECMD_FILE_PRINTK: + case TRACECMD_FILE_CMD_LINES: + case TRACECMD_FILE_CPU_COUNT: + if (current_state == (new_state - 1)) + return true; + break; + case TRACECMD_FILE_OPTIONS: + if (current_state == (new_state - 1)) + return true; + break; + case TRACECMD_FILE_CPU_LATENCY: + case TRACECMD_FILE_CPU_FLYRECORD: + if (current_state == TRACECMD_FILE_OPTIONS) + return true; + break; + } + + return false; +} + From patchwork Fri Sep 10 13:47:45 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: 12485159 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 B2946C43217 for ; Fri, 10 Sep 2021 13:48:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9CDF8611EF for ; Fri, 10 Sep 2021 13:48:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233706AbhIJNtY (ORCPT ); Fri, 10 Sep 2021 09:49:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233809AbhIJNtV (ORCPT ); Fri, 10 Sep 2021 09:49:21 -0400 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 725C0C061757 for ; Fri, 10 Sep 2021 06:48:10 -0700 (PDT) Received: by mail-ed1-x52c.google.com with SMTP id c22so1200888edn.12 for ; Fri, 10 Sep 2021 06:48:10 -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=KZqHj4I/ZwlfJwtnuzg0Oif+xGCCWXvcMAI8x2GWU7M=; b=Ao/3yF/3OVTVrRpqZI3p9vTchGyMoc8rQ6/4yZX5gtofxKll9ZCuX+hPadwb19vyGu YyuUANuNYMabQvZY3AP3GQf92G/3yvZRXCOrSg30WghWVaQgNGmLj5eMBjCk0g4IowaQ WKVy2xVfddXxBWwqCMER3dB31vats1vMkMHe1mCFRIqh3bCohhNk2mN/FsPnBiEvYIYN WJG4OMkNmu9erHLrh9GqHUcOSOA/WCBNgz0orWhi9xllQGyKREjbtj81weIKvmdYxfqa uOxIoAwLdPO7hgy7lWMQXc/OlU0GzTBpCoFnvc2S/kPgZWeZ8T4u8qCyC84OelqXIQOm 2MaQ== 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=KZqHj4I/ZwlfJwtnuzg0Oif+xGCCWXvcMAI8x2GWU7M=; b=HTxzzj6eUH5o8SP1Cfqo7oWL79WJ/x0gVOiNlGzgpX2Uvw3rgyYdv/kBKz24pBrd4r 3pXQRfeSw0kceprYCn0j9QmTJyxs0zYNaXiDpiWg4m6x89zzhP5G4PH3/FgfViMf+rFi l8c4xCiaeOJHn2GCBEab1+KlwJwdiXp8pYyYiMEou2H7EgIZLd4zBzLxRXvWwE0vRFwu YdR4E8JNu1YJdE+64fppuC8CnwvZJ9KyrxKOAjJk4wpywHHNqZjlbVB5YP4smF/vM3oB QozLQfc/OQsTNDzZOWDCa0aIUu/a2Mp6o/pPoxjdkTGTM9mZCBQ1nS2vC9uYH7Tgsw+E iuDQ== X-Gm-Message-State: AOAM533z31Quku3BZcA0Zy06dWETuth/ooa9XfVzZCoRiucC8iX41/w+ 2o3xO2tjEbrClEnBcDWLbY9b8avuf6M= X-Google-Smtp-Source: ABdhPJy9RpuWb8m/ucXNL1NsWReJHnnKDbNj/aP5Vr/GzQBRGqzwQlN407z8eTaRQMAFS+idRq5oZg== X-Received: by 2002:aa7:df8b:: with SMTP id b11mr9017330edy.61.1631281689123; Fri, 10 Sep 2021 06:48:09 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:08 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 09/21] trace-cmd library: Fix possible memory leak in read_ftrace_files() Date: Fri, 10 Sep 2021 16:47:45 +0300 Message-Id: <20210910134757.2863982-10-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Some error paths in read_ftrace_files() may lead to a memory leak. Improved the error handling of this internal function to avoid it. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-input.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 2f749d0b..f933acc6 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -616,28 +616,30 @@ static int read_ftrace_files(struct tracecmd_input *handle, const char *regex) } } - if (read4(handle, &count) < 0) - return -1; + ret = read4(handle, &count); + if (ret < 0) + goto out; for (i = 0; i < count; i++) { - if (read8(handle, &size) < 0) - return -1; + ret = read8(handle, &size); + if (ret < 0) + goto out; ret = read_ftrace_file(handle, size, print_all, ereg); if (ret < 0) - return -1; + goto out; } handle->event_files_start = lseek64(handle->fd, 0, SEEK_CUR); - + handle->file_state = TRACECMD_FILE_FTRACE_EVENTS; + ret = 0; +out: if (sreg) { regfree(sreg); regfree(ereg); } - handle->file_state = TRACECMD_FILE_FTRACE_EVENTS; - - return 0; + return ret; } static int read_event_files(struct tracecmd_input *handle, const char *regex) From patchwork Fri Sep 10 13:47:46 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: 12485163 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 44183C4332F for ; Fri, 10 Sep 2021 13:48:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 213B4611C9 for ; Fri, 10 Sep 2021 13:48:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233703AbhIJNtZ (ORCPT ); Fri, 10 Sep 2021 09:49:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233689AbhIJNtW (ORCPT ); Fri, 10 Sep 2021 09:49:22 -0400 Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B628C061574 for ; Fri, 10 Sep 2021 06:48:11 -0700 (PDT) Received: by mail-ed1-x530.google.com with SMTP id 9so2565253edx.11 for ; Fri, 10 Sep 2021 06:48:11 -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=e1RNnaRfZCZmIhrSH5IKcSWViz8K0liPeRb68rbq7h0=; b=Nv0hMLGMzec6rZZQdQd4hSt+znsrJoQY3r7nC9ZNApijxzA5Ww+gWG6+3O6+xuc9qz 8NZT2gAW67Qt6A3rn6s6Ed+6zP7lXRs/urJWoxOyxWo+a0GWOAbrTeBju2NeQGltQpK8 18sTsjXKsVCG9SMiFaV6x1iOy2Sxo5IxfZDRuRHSwMrQ7POo8FWJNsPIvamSiGqk6q7u tMi9nbvQv+MTyOoNS7nuqzhvdTTNAcezSCJTtVbBjROOGmOPw963NrXaTBBzKRG8lldf 3pZP5jnpaqEs4Vc3URNy3nGw3DQnQmuO4Al5rETxIJ/okgYanBPaHi54u/T0CSnmW4oo 6Jzg== 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=e1RNnaRfZCZmIhrSH5IKcSWViz8K0liPeRb68rbq7h0=; b=kYdh0x1uecshzziQ1izM//pVy4ZIB1T7GA1ISP47hV8ssFCOXaQruLVR2vpcbkw5XH aSyX7kt8V7Rv/6VWp2BrpKEttjTE4Srz7RqIZLZEZmUpqBaUVOBs5r7z5ShUGuzmf00H CtTGCfdH3GlB2hlaYqyCjv2NhRjSmhCVmCiX+pF5eCs7blqQ2pK2xRcMjsqHETAnYTTd A11+gAVVvOulLZVPLRxt+0oG8zs4enlyseZ9hVabe1lRJqMRjBkcsytyHZ53sIFAkS5N 9OmudIXwoUFSs8HC6ejPceFFyRsRekXNZJ8taYRBVHogsy09gOzdz3IAVu6qbdskypvm erbQ== X-Gm-Message-State: AOAM530BCwxBZ4bgn/04njfV/VytG5ft0mjrlh4u5p4FoTA6OnYl4DzP QOpDc0NpEuXEq5Z0Jq9lAhTLniYe98Y= X-Google-Smtp-Source: ABdhPJxVL+d0gToKcoHGvRvEorYn1II/vnDSUw/uRNYGAZU7vQVjzqLJWBIgIB9F1Msg23AkShPYbQ== X-Received: by 2002:a05:6402:2695:: with SMTP id w21mr1244982edd.182.1631281690200; Fri, 10 Sep 2021 06:48:10 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:09 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 10/21] trace-cmd library: Fix possible memory leak in read_event_files() Date: Fri, 10 Sep 2021 16:47:46 +0300 Message-Id: <20210910134757.2863982-11-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Some error paths in read_event_files() may lead to a memory leak. Improved the error handling of this internal function to avoid it. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-input.c | 39 ++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index f933acc6..9eeb7427 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -645,7 +645,7 @@ out: static int read_event_files(struct tracecmd_input *handle, const char *regex) { unsigned long long size; - char *system; + char *system = NULL; regex_t spreg; regex_t epreg; regex_t *sreg = NULL; @@ -670,13 +670,16 @@ static int read_event_files(struct tracecmd_input *handle, const char *regex) return -1; } - if (read4(handle, &systems) < 0) - return -1; + ret = read4(handle, &systems); + if (ret < 0) + goto out; for (i = 0; i < systems; i++) { system = read_string(handle); - if (!system) - return -1; + if (!system) { + ret = -1; + goto out; + } sys_printed = 0; print_all = 0; @@ -703,39 +706,35 @@ static int read_event_files(struct tracecmd_input *handle, const char *regex) } } - if (read4(handle, &count) < 0) - goto failed; + ret = read4(handle, &count); + if (ret < 0) + goto out; for (x=0; x < count; x++) { - if (read8(handle, &size) < 0) - goto failed; + ret = read8(handle, &size); + if (ret < 0) + goto out; ret = read_event_file(handle, system, size, print_all, &sys_printed, reg); if (ret < 0) - goto failed; + goto out; } free(system); - } - - if (sreg) { - regfree(sreg); - regfree(ereg); + system = NULL; } handle->file_state = TRACECMD_FILE_ALL_EVENTS; - - return 0; - - failed: + ret = 0; + out: if (sreg) { regfree(sreg); regfree(ereg); } free(system); - return -1; + return ret; } static int read_proc_kallsyms(struct tracecmd_input *handle) From patchwork Fri Sep 10 13:47:47 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: 12485165 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 79D53C433F5 for ; Fri, 10 Sep 2021 13:48:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 66308611EF for ; Fri, 10 Sep 2021 13:48:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233689AbhIJNtZ (ORCPT ); Fri, 10 Sep 2021 09:49:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233702AbhIJNtX (ORCPT ); Fri, 10 Sep 2021 09:49:23 -0400 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D470C061574 for ; Fri, 10 Sep 2021 06:48:12 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id g21so2586315edw.4 for ; Fri, 10 Sep 2021 06:48:12 -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=iv3Lw+Gw67afDFbw6jxgNvIwogJ7PlWL0EnjJLTvFKE=; b=plpPCOAmef6sv1uBLwtAeTK9VgH3FzFCh+Mo4IAZ7DSloEgLkqlDkZuf9P3mZDrTSh EUgjR2Olu9AHgULE+MTp91eRiGcYE9ccKbylTnhCJ89TK+tJr2MH7XdHtw2wACw9CdZu 7AY2urP5LfrugSXT2EFyTEuTxM9CsLgXRmiehuo0SS+bWojc4gxiB7yhkKFNty+PyI7t /XiXrthAfOA4SQcBDj9COPsbpw+FY00XiNGeGZ7jJ+G4xBt09LswufINiyfzqPXzuCPH R9KPLjzCcjeYxmPfOx7UdrknbapqMbrsEcU8pY+yaiFodozBMYc6CLxzZuHQgtp7aEde 98Ug== 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=iv3Lw+Gw67afDFbw6jxgNvIwogJ7PlWL0EnjJLTvFKE=; b=1gshfQsvT5Q0Ld/YmpAt5z5mZAAt0h/Lw3hnxIgottVJON+CvFhoSwDkMaB/jXBYWF Rfypu/p7HhfaQiMM7m4MrbvphcZse8wjDqwpVSSzh4hK55Bg4a1qX8+aOibIiJ0z+sBO 4mQT9kZQSDSiCcpeYuxaZAwpw7DpOkYknLd61hCFBQ+lT76MzIAnm8vSNtNcbpSqLIxd FCsc1BBof5Nh/xf7wX9vUDuWF3c0C4qhOotHWdI38RpwbZFZcY2F65doESfhW04F8zPz uY1go3leZtKC4vaRpqZiYplFGC+r2Cts6AqqVq3PjfV3VDhIruhErj945G3MrhWI76rK Lihw== X-Gm-Message-State: AOAM532HWuMJRVmX/f6nHvs8bGMTzk4Gjp3Dhzyyt9ze+Zj2c9uVQsJJ r5ys1MUxjIsq2AfRlE01Po3/JHiO5vU= X-Google-Smtp-Source: ABdhPJwEX31pHtzwY5/qVnHT5zuIiqgk/QRxydTw98Esk4XWdjXvw/0SkYfLCyCkKrASzarjOwv37g== X-Received: by 2002:aa7:c988:: with SMTP id c8mr9323073edt.351.1631281691228; Fri, 10 Sep 2021 06:48:11 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:10 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 11/21] trace-cmd library: Fix possible memory leak in read_proc_kallsyms() Date: Fri, 10 Sep 2021 16:47:47 +0300 Message-Id: <20210910134757.2863982-12-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Some error paths in read_proc_kallsyms() may lead to a memory leak. Improved the error handling of this internal function to avoid it. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-input.c | 39 +++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 9eeb7427..99c332c8 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -739,34 +739,39 @@ static int read_event_files(struct tracecmd_input *handle, const char *regex) static int read_proc_kallsyms(struct tracecmd_input *handle) { - struct tep_handle *pevent = handle->pevent; + struct tep_handle *tep = handle->pevent; unsigned int size; - char *buf; + char *buf = NULL; + int ret; if (handle->file_state >= TRACECMD_FILE_KALLSYMS) return 0; - if (read4(handle, &size) < 0) - return -1; - if (!size) - return 0; /* OK? */ + ret = read4(handle, &size); + if (ret < 0) + goto out; + if (!size) { + handle->file_state = TRACECMD_FILE_KALLSYMS; + goto out; /* OK? */ + } buf = malloc(size+1); - if (!buf) - return -1; - if (do_read_check(handle, buf, size)){ - free(buf); - return -1; + if (!buf) { + ret = -1; + goto out; } - buf[size] = 0; - - tep_parse_kallsyms(pevent, buf); + ret = do_read_check(handle, buf, size); + if (ret < 0) + goto out; - free(buf); + buf[size] = 0; + tep_parse_kallsyms(tep, buf); handle->file_state = TRACECMD_FILE_KALLSYMS; - - return 0; + ret = 0; +out: + free(buf); + return ret; } static int read_ftrace_printk(struct tracecmd_input *handle) From patchwork Fri Sep 10 13: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: 12485167 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 C8EBDC433FE for ; Fri, 10 Sep 2021 13:48:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AACF2611EE for ; Fri, 10 Sep 2021 13:48:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233702AbhIJNt0 (ORCPT ); Fri, 10 Sep 2021 09:49:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233715AbhIJNtZ (ORCPT ); Fri, 10 Sep 2021 09:49:25 -0400 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB0BFC061574 for ; Fri, 10 Sep 2021 06:48:13 -0700 (PDT) Received: by mail-ed1-x52b.google.com with SMTP id g21so2586408edw.4 for ; Fri, 10 Sep 2021 06:48:13 -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=wFujmNtaKcVQR8iZfxSNAYpKjlkZd/57MjShJqqz/UU=; b=GEB6OtbsnQ8UZZ+H04I1RV7En5zW5mdy3f5nPKWALzoDN8Qrc3K9I1LtsJdEmt13I8 Gel0Lb1e5vQr3aE3M7xbh4NZoJWIyWJ6zosJUEiDZp/lDkJskYp6hWcp01lRGkXOcqxz 78kbFhTHHdpeCbWI9PJO1s+prjSOyG4Fl4FuciZKNHgq4WUKralvMfBXtwkmEvPbIUhy d59DVaiMk+UU/D1zLj8zkpo2PjOE+bU/KxPnl9EgRDLFM1zKEccV46X74quw/er2dokx LYlgBMGpmLus7jo812qSYquYblo08OY6/p92GwgTOha/BnPUALWDt92THO7avWHEl8Ms aH1A== 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=wFujmNtaKcVQR8iZfxSNAYpKjlkZd/57MjShJqqz/UU=; b=DwicB1leCToxN6GZd5vQxUrhlfOIN/rMnCFNc5k5BC0p9tqvkhCQQYYL6LepsCPqkw st/qsf3UKE/tdP8gHRERJ9JTTr+rOHwT6F+9YZyIy6ICwsGNvw0HMdGcJzHAId28/d+h dmPBERyMp3ugN9aETfn6jRwoWeougiN+A6gaOCJJMxgQcJjryk65UBa73YXC0AQrodXR H4S1E8EVpYJVtX2bLUWPxxNvOoBFapvw6amchPp1a39aDlPxkdqz7sfaA2bI11HlMZvu Q2luTlJdG0leMWQ/iOTDDSFS/mNSDYLEdhGb4DzG1LySf8I5Kg0sU+kV8lGSQpFI/zHD 2xIw== X-Gm-Message-State: AOAM5332KPclv8MJAp+4nLaZK68POskfG5FgfRIlL5J+LPBCm3+den82 hNa5N9hR+ysXzyh/UN8ZVCrq3VF2fqY= X-Google-Smtp-Source: ABdhPJz8Ne3bn/VAIbKZEyDrzaJ1vhEDzIS/DAIQKPjY2AYi5EkYh+xXtGVqeTQ1OcXr6CZTk7h2dA== X-Received: by 2002:aa7:c952:: with SMTP id h18mr9223137edt.18.1631281692529; Fri, 10 Sep 2021 06:48:12 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:11 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 12/21] trace-cmd library: Fix possible memory leak in read_ftrace_printk() Date: Fri, 10 Sep 2021 16:47:48 +0300 Message-Id: <20210910134757.2863982-13-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Some error paths in read_ftrace_printk() may lead to a memory leak. Improved the error handling of this internal function to avoid it. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-input.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 99c332c8..fd7690c6 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -777,33 +777,39 @@ out: static int read_ftrace_printk(struct tracecmd_input *handle) { unsigned int size; - char *buf; + char *buf = NULL; + int ret; if (handle->file_state >= TRACECMD_FILE_PRINTK) return 0; - if (read4(handle, &size) < 0) - return -1; - if (!size) - return 0; /* OK? */ + ret = read4(handle, &size); + if (ret < 0) + goto out; + if (!size) { + handle->file_state = TRACECMD_FILE_PRINTK; + goto out; /* OK? */ + } buf = malloc(size + 1); - if (!buf) - return -1; - if (do_read_check(handle, buf, size)) { - free(buf); - return -1; + if (!buf) { + ret = -1; + goto out; } + ret = do_read_check(handle, buf, size); + if (ret < 0) + goto out; buf[size] = 0; tep_parse_printk_formats(handle->pevent, buf); - free(buf); - handle->file_state = TRACECMD_FILE_PRINTK; + ret = 0; - return 0; +out: + free(buf); + return ret; } static int read_and_parse_cmdlines(struct tracecmd_input *handle); From patchwork Fri Sep 10 13:47:49 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: 12485169 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 18A38C43219 for ; Fri, 10 Sep 2021 13:48:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 01AE8611C9 for ; Fri, 10 Sep 2021 13:48:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233715AbhIJNt0 (ORCPT ); Fri, 10 Sep 2021 09:49:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233704AbhIJNt0 (ORCPT ); Fri, 10 Sep 2021 09:49:26 -0400 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01B24C061574 for ; Fri, 10 Sep 2021 06:48:15 -0700 (PDT) Received: by mail-ed1-x52f.google.com with SMTP id t6so1157490edi.9 for ; Fri, 10 Sep 2021 06:48:14 -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=oZ9zyduyKBQJt5X+oVV5Fl1EGbPh4yyGk9ma+Swoy2U=; b=YkOqwmM9pi+KdDmA40yf5fwbBRlA8mzGGZKQf4TtALIMqafR4//l8ALXbI7EXhsrn1 WQpDCthxVsf6ZsckquGW8WjtKrEKU1EW0SJsq/uppbJBkZy/iTGXePOtQfTktU69ByQe 3bylf0Q0mbW/drh8ATD7iz5583rRWpxQ3nYuN5RYTgRTFSOD4mbyDQrWbc244SOJG5hu eRTJQ5l0L7hiXhwgwyhaUE/wgwzC6rj7NnRVYXc/+8EcbIUe39/ct5/6eQO0A0iv83m/ oZBcB+ntv9Y/kpOhuHdPYyU6dXTS9Ua3kUneSODmvceXvafudiWZc0SC10/SEy9nZNKu glbA== 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=oZ9zyduyKBQJt5X+oVV5Fl1EGbPh4yyGk9ma+Swoy2U=; b=gATpiaT950y5ZmzmDXeRWO6wtc8/j158sSABzMHaFzUeHKHDY0louJ6RKjQc0t3Oop GHgxecItcOVC5LGo1dmVi6nd0jbjgIXk5ddIKBQIcx9d31MD1rK/oRC5cbtaDW2OAQx2 32+vLGzS/VFiC67QFFuWlOTXXZucruVa18y1HxnNcEkqCmvmcpo4evwz0XkFVWCZ1zSU RQx7E028ggt0RNbzbxGEiKRiywJv9d6bWeqK6WkZYStFIkXC0bypGxd9ZN33qtNPkJ8C oxoaTmN3MnVedLetU3ok3Mg2CAiinP5ot93V+a3LnuwL5B/soDZu2lWqPnt/LilvFplv Apkw== X-Gm-Message-State: AOAM533EwHhz1e/9DthgjX2yu2+u5GqjU8R7WflqA8k9LpavQYNnCjgh spXmVK9UL1Hfg1ZsBTmYmhbMqlgnql0= X-Google-Smtp-Source: ABdhPJyHoKF4aG9+P6/NDSQNeABUCQl/jLBgjP1o0d4sTIzDeOFbWVhGKKXnc09p2lSmrmBffmsK/A== X-Received: by 2002:a50:c949:: with SMTP id p9mr9366186edh.326.1631281693629; Fri, 10 Sep 2021 06:48:13 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:13 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 13/21] trace-cmd library: Fix possible memory leak in read_and_parse_cmdlines() Date: Fri, 10 Sep 2021 16:47:49 +0300 Message-Id: <20210910134757.2863982-14-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Some error paths in read_and_parse_cmdlines() may lead to a memory leak. Improved the error handling of this internal function to avoid it. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-input.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index fd7690c6..a23ba9c8 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -3001,20 +3001,26 @@ static int read_and_parse_cmdlines(struct tracecmd_input *handle) { struct tep_handle *pevent = handle->pevent; unsigned long long size; - char *cmdlines; + char *cmdlines = NULL; + int ret; if (handle->file_state >= TRACECMD_FILE_CMD_LINES) return 0; - if (read_data_and_size(handle, &cmdlines, &size) < 0) - return -1; + ret = read_data_and_size(handle, &cmdlines, &size); + if (ret < 0) + goto out; + if (!size) { + handle->file_state = TRACECMD_FILE_CMD_LINES; + goto out; + } cmdlines[size] = 0; tep_parse_saved_cmdlines(pevent, cmdlines); - free(cmdlines); - handle->file_state = TRACECMD_FILE_CMD_LINES; - - return 0; + ret = 0; +out: + free(cmdlines); + return ret; } static void extract_trace_clock(struct tracecmd_input *handle, char *line) From patchwork Fri Sep 10 13:47:50 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: 12485171 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 617F7C433EF for ; Fri, 10 Sep 2021 13:48:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4DCE5611C9 for ; Fri, 10 Sep 2021 13:48:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233723AbhIJNt1 (ORCPT ); Fri, 10 Sep 2021 09:49:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233704AbhIJNt1 (ORCPT ); Fri, 10 Sep 2021 09:49:27 -0400 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1821CC061574 for ; Fri, 10 Sep 2021 06:48:16 -0700 (PDT) Received: by mail-ej1-x635.google.com with SMTP id jg16so4415991ejc.1 for ; Fri, 10 Sep 2021 06:48:16 -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=NVypbYtgUHgny7wAXLnJyOQJ1yqxvkL1dP4in6bDrys=; b=f+vPdczyVEu5lMS/SmJpHxj4rJ4L/rBvE1F2gHVSBQ4h+EcSoTLeRU7jYxPKdTW7t/ AiRid3Sefp6ynFqlplpPQvDYBITSnr6Wwz7zpzmK9NIgYBZqTPWkBcdXhMso87uIvlS6 rLy0MlBrn9kgyYn/e2Cp3RAd//mnkND0/DORJ6MI1AU9aaT/QyvySZ9U21gVHbl5FZ2l MhQy1dl/S/rLylPV6yzUETQbsybytnLrgQ6TnIfmUx5AyuvJMw4Uh2d08tp0TZnBr6i4 G9nC8gafwc4Fl3Pb9IV/Om3912utN7LQdlg9Lw92oAuEmQ9qwwZHerU57sS6LZL9BuTL ovbw== 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=NVypbYtgUHgny7wAXLnJyOQJ1yqxvkL1dP4in6bDrys=; b=Y6HreRFwDLR3nJXAXwbqU8wibxBrWheK7TGWQNBiAEE3bvEhyUXhgFTw9s2Mx6Cvqc Y+iDV6u95wmSAZfGCtSXZtL3tpB6pSYcO5byxohlp2fWtfVfBkd+KOdkURfacK+INUPC zH3BngyH0GOFvVmvLxf8Zy8gKqM2tT7nOcLQ+S6bo342hv/0YfI1vbpPW3HNVmfh9K9u W9HYGT1AgGwk+j5OXgsnwCHdpwanlnn08Pd7xVkB0iFL4dL1JLih5fvlZwul/BwQWAIp sGAFCYeiDk6eokzEPxvjUYLRmkBnv65exEN2aupECvd+CjzI1H4ytpgIeQ4Yo3fDurvH XLCQ== X-Gm-Message-State: AOAM531rtllIVGCP/KYXweL2pRVo4Kf1TNYKn7gh6l+KSlbZxNLH2Pke FT0JWQ8OVIoZZAS8Do49lmB6ZLtp0Zo= X-Google-Smtp-Source: ABdhPJy1LLA/IFJlEudL2NriVg7T5EXZ8E0TOJHRnSUoJlA4b3yxbepiAAGMx7r+kf37FRTQm0rR3Q== X-Received: by 2002:a17:907:96a2:: with SMTP id hd34mr9651881ejc.113.1631281694618; Fri, 10 Sep 2021 06:48:14 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:14 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 14/21] trace-cmd library: Set input handler default values in allocation function Date: Fri, 10 Sep 2021 16:47:50 +0300 Message-Id: <20210910134757.2863982-15-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Set usecs flag by default when the input handler is allocated, it makes more sense than setting it when options are handled. This clean up is needed for the next trace file version, where multiple options sections may exist. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-input.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index a23ba9c8..fbfc0d1c 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -2677,8 +2677,6 @@ static int handle_options(struct tracecmd_input *handle) int cpus; int ret; - /* By default, use usecs, unless told otherwise */ - handle->flags |= TRACECMD_FL_IN_USECS; handle->options_start = lseek64(handle->fd, 0, SEEK_CUR); for (;;) { @@ -3312,6 +3310,8 @@ struct tracecmd_input *tracecmd_alloc_fd(int fd, int flags) handle->fd = fd; handle->ref = 1; + /* By default, use usecs, unless told otherwise */ + handle->flags |= TRACECMD_FL_IN_USECS; if (do_read_check(handle, buf, 3)) goto failed_read; From patchwork Fri Sep 10 13:47:51 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: 12485173 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 673EBC433F5 for ; Fri, 10 Sep 2021 13:48:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4DFDE611C9 for ; Fri, 10 Sep 2021 13:48:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233725AbhIJNt2 (ORCPT ); Fri, 10 Sep 2021 09:49:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233704AbhIJNt2 (ORCPT ); Fri, 10 Sep 2021 09:49:28 -0400 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11143C061574 for ; Fri, 10 Sep 2021 06:48:17 -0700 (PDT) Received: by mail-ed1-x534.google.com with SMTP id q3so2591634edt.5 for ; Fri, 10 Sep 2021 06:48:16 -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=DhQmJDJGkcQ7eXfaCIDlchysjzm62x+Y5uYIVAkMUV4=; b=OfmE81OOUuN7cOMXc/F0qzerkM0eVyo2diA3UvY7RtsIugHS/GzgyA4qLkQJ9Nw70D tVTXxB7k1lc0EGAnsT7KfKRwzeqb0ojJ6XtOvyc3ct+Ku+L3md5JJqbtqFO9hGwj1BEb iVNcffIs2YLmKTleI26e28vxNDjcp6AnIhrclUt+78WnLr2uCcBaxILLLvJekUS8gppz FVfFUjxjBd8NH7VptaEDN9YJ6N469F5H/OZ2PTvcWcjN8f9Uk1LwUFryTRR+Q6d/6dix ZY4yHp1sbbOCrbGlw/8PM10UvvYofckoSpkbIck7hvt3/f1ubVzRtAfdtT3Y4HqWY9dj FqbA== 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=DhQmJDJGkcQ7eXfaCIDlchysjzm62x+Y5uYIVAkMUV4=; b=jxEx9tkmhRlRFo6PvYReFsU+oTgAFHjm3RhTsJuttEKc1whYFw4HtykTLDBEjc+LAy u8dGgsUqjhz2ZEVwO68YadQXMHfG0IxnwjKLJCJy7Akewj26Gm0z8rhpQJW9XGFLc4Mp /tsLTKfsL84jLwfe5VY5VjAAnyMvl+ASw6xWF2np99eaTG/v1rymD8OO0bXQMNEHPhty lBLHRM1qcFWznr47hDozm3704g43LYYfu8bgO9mCAxAhNBv4SWSGQsgxE0KdN159ijIG IKh7+Vf+pBysiauOA7mcOfGG/NLxQ563C/pZwoW+jYEKXqSw97v26TV5UfTpMtwV/2Yv 2cCQ== X-Gm-Message-State: AOAM5332dCa/ilbKcAnYMvtRpl/Dtli9qv15xcOYlhvy93ZdvdjfY2Wr 6dzXSqb5tcgNJuq0dKDrxlmCmhX0Vek= X-Google-Smtp-Source: ABdhPJz7K77h5eSDg9ASeyJqR3HeHnPHMrn7+OZnG/F5DCtsojokzxDk99zYhlrKfcfD37/jDwPaLw== X-Received: by 2002:aa7:d710:: with SMTP id t16mr8941885edq.42.1631281695682; Fri, 10 Sep 2021 06:48:15 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:15 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 15/21] trace-cmd library: Track maximum CPUs count in input handler Date: Fri, 10 Sep 2021 16:47:51 +0300 Message-Id: <20210910134757.2863982-16-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org This clean up is needed for the design of the next trace file version, where only CPUs with trace data could be stored in the file. Each trace instance may have its own CPU count, depending on collected traces. As the main input handler is used by the top trace instance, the CPU count there is for the top trace instance and may differ with cpu counts of the other instances. Added a new "max_cpu" member of the input handler, that tracks the maximum CPU count of all instances, recorded in the file. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-input.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index fbfc0d1c..48f6bbd8 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -125,6 +125,7 @@ struct tracecmd_input { int long_size; int page_size; int page_map_size; + int max_cpu; int cpus; int ref; int nr_buffers; /* buffer instances */ @@ -838,6 +839,7 @@ static int read_cpus(struct tracecmd_input *handle) return -1; handle->cpus = cpus; + handle->max_cpu = cpus; tep_set_cpus(handle->pevent, handle->cpus); handle->file_state = TRACECMD_FILE_CPU_COUNT; @@ -2794,6 +2796,8 @@ static int handle_options(struct tracecmd_input *handle) case TRACECMD_OPTION_CPUCOUNT: cpus = *(int *)buf; handle->cpus = tep_read_number(handle->pevent, &cpus, 4); + handle->max_cpu = handle->cpus; + tep_set_cpus(handle->pevent, handle->cpus); break; case TRACECMD_OPTION_PROCMAPS: if (buf[size-1] == '\0') @@ -4076,7 +4080,7 @@ int tracecmd_page_size(struct tracecmd_input *handle) */ int tracecmd_cpus(struct tracecmd_input *handle) { - return handle->cpus; + return handle->max_cpu; } /** From patchwork Fri Sep 10 13:47:52 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: 12485175 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 7868EC433FE for ; Fri, 10 Sep 2021 13:48:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 63C83611CC for ; Fri, 10 Sep 2021 13:48:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233739AbhIJNt3 (ORCPT ); Fri, 10 Sep 2021 09:49:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233704AbhIJNt3 (ORCPT ); Fri, 10 Sep 2021 09:49:29 -0400 Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A489C061574 for ; Fri, 10 Sep 2021 06:48:18 -0700 (PDT) Received: by mail-ed1-x52a.google.com with SMTP id r7so2590678edd.6 for ; Fri, 10 Sep 2021 06:48:18 -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=RL9NU2QU3eQCBHNiwR5XAbT/oZdzMq72Opi9smJDKmw=; b=QtPFo6s6nJZfonX14ru+YUX2cO1DFPe6QZiIwH9IRqVFgaXamZANgTi87wyH6WzdgW 4jTxqnystGzozBo0luuuLmfwO2YikfwT5HLjMhRbSyFIoYHtzMakX2W202Tes6IPdyIs i+a9cJpDQJqqe5QXmagOQZCF6UgnS2g7Qyx/lecRUFqDpMj6QSjvkUcJnpuw5cSKC1Fe D9IeSKWvez2hf4c2MRASTE9QJZDivB0H4eCaigN+ISYBXKTvaI6CpRX0SJtasxETP1Oy m2x8yweCZD9wKPy3/dU/WdEhL4ZGrj4nApK9fKmMx+XrpqOmMXEj2Xx1uZiEmM3FcCeC jR7Q== 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=RL9NU2QU3eQCBHNiwR5XAbT/oZdzMq72Opi9smJDKmw=; b=DP5TSTy3g/lo8j01zPcNTB/NGB23vzEVKgkABO0Bdela1qrHySBM/M7YjLrcdcZf7n r87uY6lNQyCoPzsAmUtxrNKXfTE9uJM5LDSLoopfsu2QCuHQPO2g9H7nzjAtdN6a1UEm x8ys5MlI6By7qt7a2vge3mPkoQaJfvtmhRuCxotkN4HMlAY7ytZNYPphIEHrB/3mvp0w cR4jIId8E3uw/0t9jjYjHes8+OIk0BmTG+DLn9g2l/HFwjJiiuSYOQ5nPul1wkjf7Hrq 4hAl5/9pPk6jz9tgUidlDtoLuy9o+w7tDnDxt02GWuP/Zz+6er46NbCHYNpZUruvcpnY dYmw== X-Gm-Message-State: AOAM531LC8RKtSnPR9/dOh4jat3iCqgSLhZhrVU2JstQPn0Z4fRcZwP3 rARSSYVCDokDuwMnL5ceNg2Wleivyn4= X-Google-Smtp-Source: ABdhPJzi0gJHq6mc6cJTtAB/OvtWDtWxBGpYqetlNjvjgpZHYWfZ+PysSh/dVp1/3uBf8BbFXxKUpw== X-Received: by 2002:aa7:cb8a:: with SMTP id r10mr9003655edt.237.1631281696878; Fri, 10 Sep 2021 06:48:16 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:16 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 16/21] trace-cmd report: Close input file handlers on exit Date: Fri, 10 Sep 2021 16:47:52 +0300 Message-Id: <20210910134757.2863982-17-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org When "trace-cmd report" is interrupted with "ctrl-c", close the input handlers to opened trace files, to delete any temporary files used when reading the trace data. This clean up is needed for the design of the next version of the trace file. Signed-off-by: Tzvetomir Stoyanov (VMware) --- tracecmd/trace-read.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tracecmd/trace-read.c b/tracecmd/trace-read.c index 31724b09..4261088d 100644 --- a/tracecmd/trace-read.c +++ b/tracecmd/trace-read.c @@ -1363,7 +1363,14 @@ struct tracecmd_input *read_trace_header(const char *file, int flags) static void sig_end(int sig) { + struct handle_list *handles; + fprintf(stderr, "trace-cmd: Received SIGINT\n"); + + list_for_each_entry(handles, &handle_list, list) { + tracecmd_close(handles->handle); + } + exit(0); } From patchwork Fri Sep 10 13:47:53 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: 12485177 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 D6883C433EF for ; Fri, 10 Sep 2021 13:48:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BBE4C611CC for ; Fri, 10 Sep 2021 13:48:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233777AbhIJNtc (ORCPT ); Fri, 10 Sep 2021 09:49:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233730AbhIJNta (ORCPT ); Fri, 10 Sep 2021 09:49:30 -0400 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E09DC061574 for ; Fri, 10 Sep 2021 06:48:19 -0700 (PDT) Received: by mail-ej1-x633.google.com with SMTP id ho42so4352476ejc.9 for ; Fri, 10 Sep 2021 06:48:19 -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=vFXaQI/G0HHdHdRSAHOmVE4YjkrR1Z9WT14pKr2Ncd0=; b=KpOPyNn3Z4QB7SZ6kMdu/8vcgrgVHX8ywYiSN4Nj7QFRc3wu9n0MunVAtd85aUzqpr qHDnQy5L0JMM9mTXaxL4OgozRjC4SG6NLPwEnT4ZQAHY53hT+Gu5ZcJpTLWKTsbKS8N/ H5nBVhrIdSpQahLmKKZPHX4rrcHzdPp1IEoTFHDAsM3mHTQADUyIETS/VxZCMcY/HeTa FfFpLJ19q9oRx9dn3TIa2f661z29r1dp8vUO2YNIL1zNVUsReBJYMvGm4gTQEnYjGcC+ 8JdS2fKpu7HEK5emH7ciQ5htXggrxNruT82OCXMNtd6RpmDLo8f0xel5NAFN/5WxHrRq e3zg== 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=vFXaQI/G0HHdHdRSAHOmVE4YjkrR1Z9WT14pKr2Ncd0=; b=Ga3Tuc/DKkZulfXDxP/AUEdcJAehfGxlx61sqWGlD4r7/6NLrfMy1IY7Wz/U1ohjNN oJ7S43CF3+bGE7po88/i0k13pU1w04bGjUuJ8byMU+A4zvPvTN4AJ6E5fdk+vn2G1HdH XQyvwCgUCvtq++V/kl+rhlEUidbUi6MUWwo+u1HAZpBBlBi7m8QKyue0LuFH7zkF3yAV wOyGbshhhOIG5Nn9q0bJO1H/SJgD0pGwppaGshIbLKW6MURuD5q8PibrCW6S/CEzG9su 1kpr7Up6+L3JXHyVTgk8wG/80615eO//LXwDVtKI2GHiQ6qz2A3AVNJ5S6VgpNOsi2x8 l2jg== X-Gm-Message-State: AOAM530xwyeHi7H5hEtsmLqW289oqnqklfxF8CwY70lqNKNXMsmpLk3B DANms8zCN2Txqj9CygKrddRUjFOJ6zU= X-Google-Smtp-Source: ABdhPJyXVN2mpcYma3d2gITGH8J2xhTyD4Ico/RmiZptLhoaXKt93szNi3TKmxuL1eD3UyxRLQq1rA== X-Received: by 2002:a17:906:3854:: with SMTP id w20mr9184860ejc.537.1631281697843; Fri, 10 Sep 2021 06:48:17 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:17 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 17/21] trace-cmd report: Do not print empty buffer name Date: Fri, 10 Sep 2021 16:47:53 +0300 Message-Id: <20210910134757.2863982-18-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org This clean up is needed for the design of the next version of the trace file, where the top buffer is saved with its empty file name, string "". Signed-off-by: Tzvetomir Stoyanov (VMware) --- tracecmd/trace-read.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tracecmd/trace-read.c b/tracecmd/trace-read.c index 4261088d..02f75cb9 100644 --- a/tracecmd/trace-read.c +++ b/tracecmd/trace-read.c @@ -1173,7 +1173,7 @@ static void print_handle_file(struct handle_list *handles) /* Only print file names if more than one file is read */ if (!multi_inputs && !instances) return; - if (handles->file) + if (handles->file && *handles->file != '\0') printf("%*s: ", max_file_size, handles->file); else printf("%*s ", max_file_size, ""); From patchwork Fri Sep 10 13:47:54 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: 12485179 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 030DCC433FE for ; Fri, 10 Sep 2021 13:48:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E032E611CC for ; Fri, 10 Sep 2021 13:48:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233704AbhIJNtc (ORCPT ); Fri, 10 Sep 2021 09:49:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233767AbhIJNtb (ORCPT ); Fri, 10 Sep 2021 09:49:31 -0400 Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6781DC061574 for ; Fri, 10 Sep 2021 06:48:20 -0700 (PDT) Received: by mail-ed1-x533.google.com with SMTP id i6so2604778edu.1 for ; Fri, 10 Sep 2021 06:48:20 -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=sbSV29F6hMmCzTMdZJrfrzNUd5KOIS4lhaTklr8yKGw=; b=Pe9gqQIRLBp15LdMumN9DCBSWprwFQb0FGdOoKoXTWgmtsv0C8Qev+OhUHW9HY/Q80 aR+7VKj1LhXprIaP3wOFCd967Nx/VoCMXL0V+JJwzXIhXF1AiVGg/qthTz7g4+GIY1KJ i+hSnJk562TIFaX81Dv0iswgcZe2/Qwo+rpsQzc/0msPI2oZBajtfb8B8TYpeQNRn21l dbZo1dh1rsnL8sIFK1KiQYVWvIa7tRKn8KoTDwaxblSkCVp/wl4IOw+45PS4tvsWsWLX 4lBIqboqeG+X9g1pfDfkNLKYY1H0Flu2odqZIOzmybKk2BftbBX8iji3ukZPc4tNua7o V/AA== 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=sbSV29F6hMmCzTMdZJrfrzNUd5KOIS4lhaTklr8yKGw=; b=hZmpvE/h8iTWLE57bzBo7eyKhlkhVOSUhn992UiWUGDBo/b4coVjCKGYzXYCcGsDYh She5M2N77bC/M7jLjhagU2Vxltt3zUfKXNevLL1V0coB5Rdf6WOTJiVuS7o4BHkV5S3x BvjkE+zhblzHhIZpCkJrRgx6XHepEVNfjj7mulv00tjBgX7tMk2jMq8/cqUOjhZtfo4W A+RQdEwFb2bwqzTEmfu7mfkChSYLWEvHyQWPmmPtzsuj1LzHg9PpjSCyE2r1Q9zG99i1 ec3+rEz7odCcVrDAP1UqvnVrRoA1KRkDTyfAnGQIE9VwrMsCksrQvuu6J1wDspm7fu1n gnYg== X-Gm-Message-State: AOAM532zfngvzid/8xIq3rqOTsQflPrpFPNjiWOPLtqohe9LtIQnmIiD NdbYIBzVXp+WY3G1OyVEEwEkJhWbuoI= X-Google-Smtp-Source: ABdhPJzO0COAAzRqV/Ugo20p0oeEo39pNJvJcViL9yAT9dHZKSvpYVMjyHAe4cyuGLindbLL2TkYsQ== X-Received: by 2002:aa7:d7c8:: with SMTP id e8mr2491751eds.381.1631281699045; Fri, 10 Sep 2021 06:48:19 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:18 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 18/21] trace-cmd library: Set correct CPU to the record, retrieved with tracecmd_peek_data Date: Fri, 10 Sep 2021 16:47:54 +0300 Message-Id: <20210910134757.2863982-19-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org This clean up is needed for the design of the next version of the trace file, where only CPUs with trace data are stored in the file - empty CPUs are omitted. Changed tracecmd_peek_data() to use the CPU id, instead of the CPU index. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-input.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 48f6bbd8..48931f2c 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -2019,7 +2019,7 @@ read_again: record->ts = handle->cpu_data[cpu].timestamp; record->size = kbuffer_event_size(kbuf); - record->cpu = cpu; + record->cpu = handle->cpu_data[cpu].cpu; record->data = data; record->offset = handle->cpu_data[cpu].offset + index; record->missed_events = kbuffer_missed_events(kbuf); From patchwork Fri Sep 10 13:47:55 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: 12485181 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 3DCA7C433F5 for ; Fri, 10 Sep 2021 13:48:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1EFAE611CC for ; Fri, 10 Sep 2021 13:48:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233767AbhIJNtd (ORCPT ); Fri, 10 Sep 2021 09:49:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48020 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233582AbhIJNtd (ORCPT ); Fri, 10 Sep 2021 09:49:33 -0400 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BCFADC061574 for ; Fri, 10 Sep 2021 06:48:21 -0700 (PDT) Received: by mail-ej1-x62c.google.com with SMTP id t19so4367503ejr.8 for ; Fri, 10 Sep 2021 06:48:21 -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=8Zt0veOqDDSXpzCVbb+xlRgowJ7dD1XDzcMmVcCpCxA=; b=af9FyzLPMigVwz0ngmYbV6vdYVxQw0ZblWjHUb82VCQjBnyn4zk1Oii5SDOK6ksqVH XnGPBmYZS0IMsKBokP2Bsi7seK8hJkN7Hv8vcX14sagjQ+KpRmT0eLognGewTqTkh+kR 4T9cd6U/D/2Dx+tLnslwSTfAD8EjTNpR2wFfsDdcb8F6Zlr0CnBED0E70ogG8WG/VrPm AMl0mcB5Zu+VfceL9M9TEjsWzpgoWt2J+CfbdmUDi93iwGhpJHsLadnvfDTY+t/OAaSo 3jVzqt6YiwFrObD6KsepL983t8oqzlqTypS5Im4zV4P6pDlUsSVFp+QKGX0qV1jWlO/d 9zOA== 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=8Zt0veOqDDSXpzCVbb+xlRgowJ7dD1XDzcMmVcCpCxA=; b=aXFfDDH6oM2ugtiuqLdM6xsEPKmjrkoNo2niNiDxWZWjfOGfWWPPLH3jd5lXJYa1fc lX4SVu0y4ZmQO1IClI64XxeSfMWvu5co8wtKlnwKb6+D1RC0SmcaV/HZMbQTunVyyCFt mSrXCgJQFEuGLg741cdJSxgfuZEgcDDireruFIo17MVoo+kbmaXtwDOFNop8Ec8SmZfA Dn1Hvk9l0GlJDjaGbfWiQDjdeCeixwFv76r7Q2bQ172x3YJWNfFogKyLRqCiaduNC73F O+bdy8nOUPH4V6/kPZ/FDOYa+S7f5TiSE5l7ZUASaP6S1eNnof7bo7EKE1goES7BMlhp b+AQ== X-Gm-Message-State: AOAM531jxMNmeSe/0cUeUHG6qxgYOSSjaPl3GoFBPQwwwdfQDVGbquNm KyYM93zz2vEbdB4YI6nCSoEWIYUY2A8= X-Google-Smtp-Source: ABdhPJyJt4/V694sCbqsyvBcrUM7Amh8r9jJVJ88H2/l6p1a50Nzen9aH/dFcIcVOJ2R8hEDyrgJBA== X-Received: by 2002:a17:906:a0a:: with SMTP id w10mr9814574ejf.416.1631281700237; Fri, 10 Sep 2021 06:48:20 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:19 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 19/21] trace-cmd library: Refactor APIs for creating output handler Date: Fri, 10 Sep 2021 16:47:55 +0300 Message-Id: <20210910134757.2863982-20-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org In the trace-cmd library there are various APIs for allocating and initializing output handler to a trace file. The existing APIs are use case oriented, with a lot of parameters. Extending them for new use cases, adding more input parameters, will make the library more complex and not easy to use. Almost all use case oriented APIs for output handler creation are removed and replaced with new flow, which is easier to be extended with new creation parameters. Removed APIs: tracecmd_create_init_fd_msg() tracecmd_create_init_file_glob() tracecmd_create_init_fd_glob() tracecmd_create_init_file_override() New APIs: tracecmd_output_allocate() tracecmd_output_set_msg() tracecmd_output_set_trace_dir() tracecmd_output_set_kallsyms() tracecmd_output_set_from_input() tracecmd_output_write_init() tracecmd_output_write_headers() The new tracecmd_output_allocate() API allocates memory and performs minimal initialization of an output handler to a trace file. No data is written in the file. The tracecmd_output_set_...() APIs can be used to set various parameters to the newly allocated output handler, that affect the way the data is written into the file. When the output handler is configured for the desired use case, the tracecmd_output_write_init() is used to start writing to the file, it writes initial magic bytes. The tracecmd_output_write_headers() API is used to write the initial headers into the file. Signed-off-by: Tzvetomir Stoyanov (VMware) --- .../include/private/trace-cmd-private.h | 25 +- lib/trace-cmd/trace-output.c | 406 ++++++++++++------ tracecmd/trace-record.c | 55 ++- tracecmd/trace-restore.c | 32 +- 4 files changed, 356 insertions(+), 162 deletions(-) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index c58b09e9..167f3804 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -102,7 +102,8 @@ static inline int tracecmd_host_bigendian(void) /* --- Opening and Reading the trace.dat file --- */ enum tracecmd_file_states { - TRACECMD_FILE_INIT = 1, + TRACECMD_FILE_ALLOCATED = 0, + TRACECMD_FILE_INIT, TRACECMD_FILE_HEADERS, TRACECMD_FILE_FTRACE_EVENTS, TRACECMD_FILE_ALL_EVENTS, @@ -268,20 +269,20 @@ struct tracecmd_event_list { struct tracecmd_option; struct tracecmd_msg_handle; +struct tracecmd_output *tracecmd_output_allocate(int fd); +int tracecmd_output_set_msg(struct tracecmd_output *handler, + struct tracecmd_msg_handle *msg_handle); +int tracecmd_output_set_trace_dir(struct tracecmd_output *handler, const char *tracing_dir); +int tracecmd_output_set_kallsyms(struct tracecmd_output *handler, const char *kallsyms); +int tracecmd_output_set_from_input(struct tracecmd_output *handler, struct tracecmd_input *ihandle); +int tracecmd_output_write_init(struct tracecmd_output *handler); +int tracecmd_output_write_headers(struct tracecmd_output *handler, + struct tracecmd_event_list *list); + struct tracecmd_output *tracecmd_create_file_latency(const char *output_file, int cpus); -struct tracecmd_output * -tracecmd_create_init_file_glob(const char *output_file, - struct tracecmd_event_list *list); struct tracecmd_output *tracecmd_create_init_fd(int fd); -struct tracecmd_output * -tracecmd_create_init_fd_glob(int fd, struct tracecmd_event_list *list); -struct tracecmd_output * -tracecmd_create_init_fd_msg(struct tracecmd_msg_handle *msg_handle, - struct tracecmd_event_list *list); + struct tracecmd_output *tracecmd_create_init_file(const char *output_file); -struct tracecmd_output *tracecmd_create_init_file_override(const char *output_file, - const char *tracing_dir, - const char *kallsyms); struct tracecmd_option *tracecmd_add_option(struct tracecmd_output *handle, unsigned short id, int size, const void *data); diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index fe1d1aaa..20116d99 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -31,11 +31,6 @@ typedef unsigned long long tsize_t; typedef long long stsize_t; -static struct tracecmd_event_list all_event_list = { - .next = NULL, - .glob = "all" -}; - struct tracecmd_option { unsigned short id; int size; @@ -54,11 +49,14 @@ struct tracecmd_output { int cpus; struct tep_handle *pevent; char *tracing_dir; + char *kallsyms; + struct tracecmd_event_list *events; int nr_options; bool quiet; unsigned long file_state; unsigned long file_version; size_t options_start; + bool big_endian; struct list_head options; struct tracecmd_msg_handle *msg_handle; @@ -682,7 +680,7 @@ static int read_event_files(struct tracecmd_output *handle, break; } /* all events are listed, use a global glob */ - if (list) + if (!event_list || list) event_list = &all_events; systems = create_event_list(handle, event_list); @@ -752,8 +750,7 @@ err: tracecmd_warning("can't set kptr_restrict"); } -static int read_proc_kallsyms(struct tracecmd_output *handle, - const char *kallsyms) +static int read_proc_kallsyms(struct tracecmd_output *handle) { unsigned int size, check_size, endian4; const char *path = "/proc/kallsyms"; @@ -766,8 +763,8 @@ static int read_proc_kallsyms(struct tracecmd_output *handle, return -1; } - if (kallsyms) - path = kallsyms; + if (handle->kallsyms) + path = handle->kallsyms; ret = stat(path, &st); if (ret < 0) { @@ -883,137 +880,247 @@ out_free: return ret; } -static int select_file_version(struct tracecmd_output *handle, - struct tracecmd_input *ihandle) -{ - if (ihandle) - handle->file_version = tracecmd_get_in_file_version(ihandle); - else - handle->file_version = FILE_VERSION; - - return 0; -} - -static struct tracecmd_output * -create_file_fd(int fd, struct tracecmd_input *ihandle, - const char *tracing_dir, - const char *kallsyms, - struct tracecmd_event_list *list, - struct tracecmd_msg_handle *msg_handle) +/** + * tracecmd_output_allocate - allocate new output handler to a trace file + * @handle: file descriptor to an empty file, it can be -1 if the handler + * will not write to a file + * + * This API only allocates a handler and performs minimal initialization. + * No data is written in the file. + * + * Returns pointer to a newly allocated file descriptor, that can be used + * with other library APIs. In case of an error, NULL is returned. The returned + * handler must be freed with tracecmd_output_close() or tracecmd_output_free() + */ +struct tracecmd_output *tracecmd_output_allocate(int fd) { struct tracecmd_output *handle; - struct tep_handle *pevent; - char buf[BUFSIZ]; - int endian4; - handle = malloc(sizeof(*handle)); + handle = calloc(1, sizeof(*handle)); if (!handle) return NULL; - memset(handle, 0, sizeof(*handle)); - - list_head_init(&handle->options); handle->fd = fd; - if (tracing_dir) { - handle->tracing_dir = strdup(tracing_dir); - if (!handle->tracing_dir) - goto out_free; - } - handle->msg_handle = msg_handle; + handle->file_version = FILE_VERSION; - if (select_file_version(handle, ihandle)) - goto out_free; + handle->page_size = getpagesize(); - buf[0] = 23; - buf[1] = 8; - buf[2] = 68; - memcpy(buf + 3, "tracing", 7); + if (tracecmd_host_bigendian()) + handle->big_endian = true; + else + handle->big_endian = false; - if (do_write_check(handle, buf, 10)) - goto out_free; + list_head_init(&handle->options); - sprintf(buf, "%lu", handle->file_version); - if (do_write_check(handle, buf, strlen(buf) + 1)) - goto out_free; + handle->file_state = TRACECMD_FILE_ALLOCATED; - /* get endian and page size */ - if (ihandle) { - pevent = tracecmd_get_tep(ihandle); - /* Use the pevent of the ihandle for later writes */ - handle->pevent = tracecmd_get_tep(ihandle); - tep_ref(pevent); - if (tep_is_file_bigendian(pevent)) - buf[0] = 1; - else - buf[0] = 0; - handle->page_size = tracecmd_page_size(ihandle); - } else { - if (tracecmd_host_bigendian()) - buf[0] = 1; - else - buf[0] = 0; - handle->page_size = getpagesize(); - } + return handle; +} - if (do_write_check(handle, buf, 1)) - goto out_free; +/** + * tracecmd_output_set_msg - associated an output file handler with network message handler + * @handle: output handler to a trace file. + * @msg_handle: network handler, allocated by tracecmd_msg_handle_alloc() + * + * This API associates an output file handler with a network stream. All subsequent API calls + * with this output file handler will send data over the network using the @msg_handle, instead + * of writing to a file. + * This API must be called after the handler file version is set and before + * tracecmd_output_write_init(). + * + * Returns 0 on success, or -1 if the output file handler is not allocated or not in expected state. + */ +int tracecmd_output_set_msg(struct tracecmd_output *handler, struct tracecmd_msg_handle *msg_handle) +{ + if (!handler || handler->file_state != TRACECMD_FILE_ALLOCATED) + return -1; - /* save size of long (this may not be what the kernel is) */ - buf[0] = sizeof(long); - if (do_write_check(handle, buf, 1)) - goto out_free; + handler->msg_handle = msg_handle; - endian4 = convert_endian_4(handle, handle->page_size); - if (do_write_check(handle, &endian4, 4)) - goto out_free; - handle->file_state = TRACECMD_FILE_INIT; + return 0; +} - if (ihandle) - return handle; +/** + * tracecmd_output_set_trace_dir - Set a custom tracing dir, instead of system default + * @handle: output handler to a trace file. + * @tracing_dir: full path to a directory with tracing files + * + * This API associates an output file handler with a custom tracing directory, to be used when + * creating the trace file instead of the system default tracing directory. + * This API must be called before tracecmd_output_write_init(). + * + * Returns 0 on success, or -1 if the output file handler is not allocated or not in expected state. + */ +int tracecmd_output_set_trace_dir(struct tracecmd_output *handler, const char *tracing_dir) +{ + if (!handler || handler->file_state != TRACECMD_FILE_ALLOCATED) + return -1; - if (read_header_files(handle)) - goto out_free; + free(handler->tracing_dir); + if (tracing_dir) { + handler->tracing_dir = strdup(tracing_dir); + if (!handler->tracing_dir) + return -1; + } else + handler->tracing_dir = NULL; - if (read_ftrace_files(handle)) - goto out_free; + return 0; +} - if (read_event_files(handle, list)) - goto out_free; +/** + * tracecmd_output_set_kallsyms - Set a custom kernel symbols file, instead of system default + * @handle: output handler to a trace file. + * @tracing_dir: full path to a file with kernel symbols + * + * This API associates an output file handler with a custom kernel symbols file, to be used when + * creating the trace file instead of the system default kernel symbols file. + * This API must be called before tracecmd_output_write_init(). + * + * Returns 0 on success, or -1 if the output file handler is not allocated or not in expected state. + */ +int tracecmd_output_set_kallsyms(struct tracecmd_output *handler, const char *kallsyms) +{ + if (!handler || handler->file_state != TRACECMD_FILE_ALLOCATED) + return -1; - if (read_proc_kallsyms(handle, kallsyms)) - goto out_free; + free(handler->kallsyms); + if (kallsyms) { + handler->kallsyms = strdup(kallsyms); + if (!handler->kallsyms) + return -1; + } else + handler->kallsyms = NULL; - if (read_ftrace_printk(handle)) - goto out_free; + return 0; +} - return handle; +/** + * tracecmd_output_set_from_input - Inherit parameters from an existing trace file + * @handle: output handler to a trace file. + * @ihandle: input handler to an existing trace file. + * + * This API copies parameters from input handler @ihandle, associated with an existing trace file, + * to the output handler @handle, associated with file that is going to be created. + * These parameters are copied: + * - tep handler + * - page size + * - file endian + * - file version + * - file compression protocol + * This API must be called before tracecmd_output_write_init(). + * + * Returns 0 on success, or -1 if the output file handler is not allocated or not in expected state. + */ +int tracecmd_output_set_from_input(struct tracecmd_output *handler, struct tracecmd_input *ihandle) +{ + if (!handler || !ihandle || handler->file_state != TRACECMD_FILE_ALLOCATED) + return -1; - out_free: - tracecmd_output_close(handle); - return NULL; + /* get endian, page size, file version and compression */ + /* Use the pevent of the ihandle for later writes */ + handler->pevent = tracecmd_get_tep(ihandle); + tep_ref(handler->pevent); + handler->page_size = tracecmd_page_size(ihandle); + handler->file_version = tracecmd_get_in_file_version(ihandle); + if (tep_is_file_bigendian(handler->pevent)) + handler->big_endian = true; + else + handler->big_endian = false; + + + return 0; } -static struct tracecmd_output *create_file(const char *output_file, - struct tracecmd_input *ihandle, - const char *tracing_dir, - const char *kallsyms, - struct tracecmd_event_list *list) +/** + * tracecmd_output_write_init - Write the initial magics in the trace file + * @handle: output handler to a trace file. + * + * This API must be called after all tracecmd_output_set_...() APIs and before writing anything + * to the trace file. This initial information is written in the file: + * - initial file magic bytes + * - file version + * - data endian + * - long size + * - page size + * - compression header + * + * Returns 0 on success, or -1 if the output file handler is not allocated or not in expected state. + */ +int tracecmd_output_write_init(struct tracecmd_output *handler) { - struct tracecmd_output *handle; - int fd; + char buf[BUFSIZ]; + int endian4; - fd = open(output_file, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, 0644); - if (fd < 0) - return NULL; + if (!handler || handler->file_state != TRACECMD_FILE_ALLOCATED) + return -1; - handle = create_file_fd(fd, ihandle, tracing_dir, kallsyms, list, NULL); - if (!handle) { - close(fd); - unlink(output_file); - } + buf[0] = 23; + buf[1] = 8; + buf[2] = 68; + memcpy(buf + 3, "tracing", 7); - return handle; + if (do_write_check(handler, buf, 10)) + return -1; + + sprintf(buf, "%lu", handler->file_version); + if (do_write_check(handler, buf, strlen(buf) + 1)) + return -1; + + if (handler->big_endian) + buf[0] = 1; + else + buf[0] = 0; + if (do_write_check(handler, buf, 1)) + return -1; + + /* save size of long (this may not be what the kernel is) */ + buf[0] = sizeof(long); + if (do_write_check(handler, buf, 1)) + return -1; + + endian4 = convert_endian_4(handler, handler->page_size); + if (do_write_check(handler, &endian4, 4)) + return -1; + handler->file_state = TRACECMD_FILE_INIT; + return 0; +} + +/** + * tracecmd_output_write_headers - Write the trace file headers + * @handle: output handler to a trace file. + * @list: desired events that will be included in the trace file. + * It can be NULL for all available events + * + * These headers are written in the file: + * - header files from the tracing directory + * - ftrace events from the tracing directory + * - event file from the tracing directory - all or only the one from @list + * - kernel symbols from the tracing directory + * - kernel printk strings from the tracing directory + * + * Returns 0 on success, or -1 in case of an error. + */ +int tracecmd_output_write_headers(struct tracecmd_output *handler, + struct tracecmd_event_list *list) +{ + if (!handler || handler->file_state < TRACECMD_FILE_ALLOCATED) + return -1; + + /* Write init data, if not written yet */ + if (handler->file_state < TRACECMD_FILE_INIT && tracecmd_output_write_init(handler)) + return -1; + if (read_header_files(handler)) + return -1; + if (read_ftrace_files(handler)) + return -1; + if (read_event_files(handler, list)) + return -1; + if (read_proc_kallsyms(handler)) + return -1; + if (read_ftrace_printk(handler)) + return -1; + return 0; } /** @@ -1267,11 +1374,19 @@ struct tracecmd_output *tracecmd_create_file_latency(const char *output_file, in { struct tracecmd_output *handle; char *path; + int fd; - handle = create_file(output_file, NULL, NULL, NULL, &all_event_list); - if (!handle) + fd = open(output_file, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, 0644); + if (fd < 0) return NULL; + handle = tracecmd_output_allocate(fd); + if (!handle) + goto out_free; + if (tracecmd_output_write_init(handle)) + goto out_free; + if (tracecmd_output_write_headers(handle, NULL)) + goto out_free; /* * Save the command lines; */ @@ -1565,39 +1680,38 @@ struct tracecmd_output *tracecmd_get_output_handle_fd(int fd) struct tracecmd_output *tracecmd_create_init_fd(int fd) { - return create_file_fd(fd, NULL, NULL, NULL, &all_event_list, NULL); -} - -struct tracecmd_output * -tracecmd_create_init_fd_msg(struct tracecmd_msg_handle *msg_handle, - struct tracecmd_event_list *list) -{ - return create_file_fd(msg_handle->fd, NULL, NULL, NULL, list, msg_handle); -} + struct tracecmd_output *out; -struct tracecmd_output * -tracecmd_create_init_fd_glob(int fd, struct tracecmd_event_list *list) -{ - return create_file_fd(fd, NULL, NULL, NULL, list, NULL); -} - -struct tracecmd_output * -tracecmd_create_init_file_glob(const char *output_file, - struct tracecmd_event_list *list) -{ - return create_file(output_file, NULL, NULL, NULL, list); + out = tracecmd_output_allocate(fd); + if (!out) + return NULL; + if (tracecmd_output_write_init(out)) + goto error; + if (tracecmd_output_write_headers(out, NULL)) + goto error; + + return out; +error: + tracecmd_output_close(out); + return NULL; } struct tracecmd_output *tracecmd_create_init_file(const char *output_file) { - return create_file(output_file, NULL, NULL, NULL, &all_event_list); -} + struct tracecmd_output *handle; + int fd; -struct tracecmd_output *tracecmd_create_init_file_override(const char *output_file, - const char *tracing_dir, - const char *kallsyms) -{ - return create_file(output_file, NULL, tracing_dir, kallsyms, &all_event_list); + fd = open(output_file, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, 0644); + if (fd < 0) + return NULL; + handle = tracecmd_create_init_fd(fd); + if (!handle) { + close(fd); + unlink(output_file); + return NULL; + } + + return handle; } /** @@ -1613,11 +1727,19 @@ struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, const char *file) { struct tracecmd_output *handle; + int fd; - handle = create_file(file, ihandle, NULL, NULL, &all_event_list); - if (!handle) + fd = open(file, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, 0644); + if (fd < 0) return NULL; + handle = tracecmd_output_allocate(fd); + if (!handle) + goto out_free; + if (tracecmd_output_set_from_input(handle, ihandle)) + goto out_free; + tracecmd_output_write_init(handle); + if (tracecmd_copy_headers(ihandle, handle->fd, 0, 0) < 0) goto out_free; diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index 1767a6c6..15e07cf0 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -3689,6 +3689,25 @@ again: static void add_options(struct tracecmd_output *handle, struct common_record_context *ctx); +static struct tracecmd_output *create_net_output(struct common_record_context *ctx, + struct tracecmd_msg_handle *msg_handle) +{ + struct tracecmd_output *out; + + out = tracecmd_output_allocate(-1); + if (!out) + return NULL; + if (tracecmd_output_set_msg(out, msg_handle)) + goto error; + if (tracecmd_output_write_headers(out, listed_events)) + goto error; + + return out; +error: + tracecmd_output_close(out); + return NULL; +} + static struct tracecmd_msg_handle * setup_connection(struct buffer_instance *instance, struct common_record_context *ctx) { @@ -3700,7 +3719,7 @@ setup_connection(struct buffer_instance *instance, struct common_record_context /* Now create the handle through this socket */ if (msg_handle->version == V3_PROTOCOL) { - network_handle = tracecmd_create_init_fd_msg(msg_handle, listed_events); + network_handle = create_net_output(ctx, msg_handle); if (!network_handle) goto error; tracecmd_set_quiet(network_handle, quiet); @@ -3718,10 +3737,11 @@ setup_connection(struct buffer_instance *instance, struct common_record_context if (ret) goto error; } else { - network_handle = tracecmd_create_init_fd_glob(msg_handle->fd, - listed_events); + network_handle = tracecmd_output_allocate(msg_handle->fd); if (!network_handle) goto error; + if (tracecmd_output_write_headers(network_handle, listed_events)) + goto error; tracecmd_set_quiet(network_handle, quiet); } @@ -4067,8 +4087,7 @@ static void setup_agent(struct buffer_instance *instance, { struct tracecmd_output *network_handle; - network_handle = tracecmd_create_init_fd_msg(instance->msg_handle, - listed_events); + network_handle = create_net_output(ctx, instance->msg_handle); add_options(network_handle, ctx); tracecmd_write_cmdlines(network_handle); tracecmd_write_cpus(network_handle, instance->cpu_count); @@ -4437,6 +4456,30 @@ static void write_guest_file(struct buffer_instance *instance) free(temp_files); } +static struct tracecmd_output *create_output(struct common_record_context *ctx) +{ + struct tracecmd_output *out; + int fd; + + fd = open(ctx->output, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, 0644); + if (fd < 0) + return NULL; + + out = tracecmd_output_allocate(fd); + if (!out) + goto error; + if (tracecmd_output_write_headers(out, listed_events)) + goto error; + return out; +error: + if (out) + tracecmd_output_close(out); + else + close(fd); + unlink(ctx->output); + return NULL; +} + static void record_data(struct common_record_context *ctx) { struct tracecmd_option **buffer_options; @@ -4491,7 +4534,7 @@ static void record_data(struct common_record_context *ctx) touch_file(temp_files[i]); } - handle = tracecmd_create_init_file_glob(ctx->output, listed_events); + handle = create_output(ctx); if (!handle) die("Error creating output file"); tracecmd_set_quiet(handle, quiet); diff --git a/tracecmd/trace-restore.c b/tracecmd/trace-restore.c index 280a37f0..8d2fcae8 100644 --- a/tracecmd/trace-restore.c +++ b/tracecmd/trace-restore.c @@ -22,6 +22,35 @@ #include "trace-local.h" +static struct tracecmd_output *create_output(const char *file, + const char *tracing_dir, const char *kallsyms) +{ + struct tracecmd_output *out; + int fd; + + fd = open(file, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, 0644); + if (fd < 0) + return NULL; + + out = tracecmd_output_allocate(fd); + if (!out) + goto error; + if (tracing_dir && tracecmd_output_set_trace_dir(out, tracing_dir)) + goto error; + if (kallsyms && tracecmd_output_set_kallsyms(out, kallsyms)) + goto error; + if (tracecmd_output_write_headers(out, NULL)) + goto error; + return out; +error: + if (out) + tracecmd_output_close(out); + else + close(fd); + unlink(file); + return NULL; +} + void trace_restore (int argc, char **argv) { struct tracecmd_output *handle; @@ -90,8 +119,7 @@ void trace_restore (int argc, char **argv) usage(argv); } - handle = tracecmd_create_init_file_override(output, tracing_dir, - kallsyms); + handle = create_output(output, tracing_dir, kallsyms); if (!handle) die("Unabled to create output file %s", output); if (tracecmd_write_cmdlines(handle) < 0) From patchwork Fri Sep 10 13:47:56 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: 12485183 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 5BB50C433EF for ; Fri, 10 Sep 2021 13:48:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3DE06611CC for ; Fri, 10 Sep 2021 13:48:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233782AbhIJNte (ORCPT ); Fri, 10 Sep 2021 09:49:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233582AbhIJNte (ORCPT ); Fri, 10 Sep 2021 09:49:34 -0400 Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B60BDC061574 for ; Fri, 10 Sep 2021 06:48:22 -0700 (PDT) Received: by mail-ed1-x531.google.com with SMTP id 9so2566047edx.11 for ; Fri, 10 Sep 2021 06:48:22 -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=gXM2bLjK+vpvadwzGvKlftuaWiUMsfFXiQadv9nvFww=; b=CdovOEkexvIWLeJeMBj09IYHMY3DUSQp4Rzo6PuG/fXfxxqgsCebVnB2vRiZk1l/gC HYMIJ75Qgg+QnTvBRKr0cGk1KXPbsURZaltEYxFQWXXaZC1MZ1FlGoPURgTKfw5PdGEQ TTLxHFIy/0GV8S+SiMtt7hqck+f2zA6hSuj9o57UoUf7vWMyVXF3SxEGzCrRQE5UGpOP v2VeEqh9NUkqiysQyRnM3XDRRIjqwb00KIJ91ijF4A7jWSxXPWNuniSRfX3qqDpZk45a yPEKa5m1IOvCGLxF73FauBJL9GUK7zvc5q5DvX8tExlm6eccU6qoB7UDz9K0o4mGpa7p GFyg== 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=gXM2bLjK+vpvadwzGvKlftuaWiUMsfFXiQadv9nvFww=; b=GWJTDdH1+tW0+8mgNP9xASAJP2IwkiVYHbnFaylnSYyJf45mkncvu3ILysjVKuMEUs zn7Nx3YwYjelSFmfYtoOByJKfs50atU+jDCVItCcOPokf68U5hzwy4w8IC2zNTg4ys3q KxpS+RJe/93B/w6dQwCSakzjAUPQ3KjSBPTSyih+qfefNz+FDJaJ21iiPiL81mhGVmFH glRIjrZz2/4wEjWbyoC6u7j2D2Y9TPFa+ZJpI6vxG3XuCb8arxIiz3BiYVnlzRsl+jAl Hyn3t5tvaoReUi3j86i6r+9ehc/xGAXk4Ayik5wTc6H4j8Op0wiJrNdmIqqRV4AC5eIS +EsQ== X-Gm-Message-State: AOAM5301K8k0iT5eEoIw6ptrkIshT99vVvEZ+MPpdwq17SIJCsm1bDLr TViBZLCq+Zr3ehJymvXCUF05pnOsQpA= X-Google-Smtp-Source: ABdhPJzonhmtiYu74LhQQfR0A2s0R1Lag7ViQwSFFiwL1CCK1TlqSLHvCTLCM62ctKmtfUWK2l66Uw== X-Received: by 2002:a05:6402:524e:: with SMTP id t14mr9126780edd.66.1631281701204; Fri, 10 Sep 2021 06:48:21 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:20 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 20/21] trace-cmd library: Refactor the logic for writing trace data in the file Date: Fri, 10 Sep 2021 16:47:56 +0300 Message-Id: <20210910134757.2863982-21-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org When a trace buffer data are written in the trace file, the buffer option in the file metadata is updated with the file offset of the tracing data. Hide this logic into the trace-cmd library. Added new APIs: tracecmd_add_buffer_info() tracecmd_write_buffer_info() Changed APIs: tracecmd_append_buffer_cpu_data() Removed APIs: tracecmd_add_buffer_option() Refactored the internal logic of tracecmd_write_cpu_data() API to be suitable for upcoming trace file format changes and data compression. The size and the offset of the trace data is saved in the file right after the data is written. The old logic calculates the size and offset in advance, but when the trace data is compressed it is hard to use that approach. Signed-off-by: Tzvetomir Stoyanov (VMware) --- .../include/private/trace-cmd-private.h | 10 +- lib/trace-cmd/include/trace-cmd-local.h | 17 + lib/trace-cmd/trace-output.c | 308 ++++++++++++------ tracecmd/trace-listen.c | 2 +- tracecmd/trace-record.c | 18 +- 5 files changed, 237 insertions(+), 118 deletions(-) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 167f3804..97cd82f8 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -290,8 +290,8 @@ struct tracecmd_option * tracecmd_add_option_v(struct tracecmd_output *handle, unsigned short id, const struct iovec *vector, int count); -struct tracecmd_option *tracecmd_add_buffer_option(struct tracecmd_output *handle, - const char *name, int cpus); +int tracecmd_add_buffer_info(struct tracecmd_output *handle, const char *name, int cpus); +int tracecmd_write_buffer_info(struct tracecmd_output *handle); int tracecmd_write_cpus(struct tracecmd_output *handle, int cpus); int tracecmd_write_cmdlines(struct tracecmd_output *handle); @@ -303,13 +303,11 @@ struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, const char *file); int tracecmd_write_cpu_data(struct tracecmd_output *handle, - int cpus, char * const *cpu_data_files); + int cpus, char * const *cpu_data_files, const char *buff_name); int tracecmd_append_cpu_data(struct tracecmd_output *handle, int cpus, char * const *cpu_data_files); int tracecmd_append_buffer_cpu_data(struct tracecmd_output *handle, - struct tracecmd_option *option, - int cpus, char * const *cpu_data_files); - + const char *name, int cpus, char * const *cpu_data_files); struct tracecmd_output *tracecmd_get_output_handle_fd(int fd); /* --- Reading the Fly Recorder Trace --- */ diff --git a/lib/trace-cmd/include/trace-cmd-local.h b/lib/trace-cmd/include/trace-cmd-local.h index 7f3b45a8..547ee5c3 100644 --- a/lib/trace-cmd/include/trace-cmd-local.h +++ b/lib/trace-cmd/include/trace-cmd-local.h @@ -31,7 +31,24 @@ void tracecmd_info(const char *fmt, ...); #endif #endif +struct data_file_write { + unsigned long long file_size; + unsigned long long write_size; + unsigned long long soffset; + unsigned long long data_offset; + unsigned long long doffset; +}; + 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); +struct cpu_data_source { + int fd; + int size; + off64_t offset; +}; + +int out_write_cpu_data(struct tracecmd_output *handle, int cpus, + struct cpu_data_source *data, const char *buff_name); + #endif /* _TRACE_CMD_LOCAL_H */ diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index 20116d99..ad18fa9f 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -39,6 +39,14 @@ struct tracecmd_option { struct list_head list; }; +struct tracecmd_buffer { + int cpus; + void *name; + tsize_t offset; + struct tracecmd_option *option; + struct list_head list; +}; + enum { OUTPUT_FL_SEND_META = (1 << 0), }; @@ -59,6 +67,7 @@ struct tracecmd_output { bool big_endian; struct list_head options; + struct list_head buffers; struct tracecmd_msg_handle *msg_handle; char *trace_clock; }; @@ -141,6 +150,7 @@ bool tracecmd_get_quiet(struct tracecmd_output *handle) void tracecmd_output_free(struct tracecmd_output *handle) { struct tracecmd_option *option; + struct tracecmd_buffer *buffer; if (!handle) return; @@ -151,6 +161,13 @@ void tracecmd_output_free(struct tracecmd_output *handle) if (handle->pevent) tep_unref(handle->pevent); + while (!list_empty(&handle->buffers)) { + buffer = container_of(handle->buffers.next, + struct tracecmd_buffer, list); + list_del(&buffer->list); + free(buffer->name); + free(buffer); + } while (!list_empty(&handle->options)) { option = container_of(handle->options.next, struct tracecmd_option, list); @@ -912,6 +929,7 @@ struct tracecmd_output *tracecmd_output_allocate(int fd) handle->big_endian = false; list_head_init(&handle->options); + list_head_init(&handle->buffers); handle->file_state = TRACECMD_FILE_ALLOCATED; @@ -1324,15 +1342,14 @@ int tracecmd_append_options(struct tracecmd_output *handle) return 0; } -struct tracecmd_option * -tracecmd_add_buffer_option(struct tracecmd_output *handle, const char *name, - int cpus) +static struct tracecmd_option * +add_buffer_option(struct tracecmd_output *handle, const char *name, int cpus) { struct tracecmd_option *option; char *buf; int size = 8 + strlen(name) + 1; - buf = malloc(size); + buf = calloc(1, size); if (!buf) { tracecmd_warning("Failed to malloc buffer"); return NULL; @@ -1354,6 +1371,55 @@ tracecmd_add_buffer_option(struct tracecmd_output *handle, const char *name, return option; } +int tracecmd_add_buffer_info(struct tracecmd_output *handle, const char *name, int cpus) +{ + struct tracecmd_buffer *buf; + + buf = calloc(1, sizeof(struct tracecmd_buffer)); + if (!buf) + return -1; + buf->name = strdup(name); + buf->cpus = cpus; + if (!buf->name) { + free(buf); + return -1; + } + list_add_tail(&buf->list, &handle->buffers); + return 0; +} + +int tracecmd_write_buffer_info(struct tracecmd_output *handle) +{ + struct tracecmd_option *option; + struct tracecmd_buffer *buf; + + if (handle->file_version >= 7) + return 0; + + list_for_each_entry(buf, &handle->buffers, list) { + option = add_buffer_option(handle, buf->name, buf->cpus); + if (!option) + return -1; + buf->option = option; + } + + return 0; +} + +static tsize_t get_buffer_file_offset(struct tracecmd_output *handle, const char *name) +{ + struct tracecmd_buffer *buf; + + list_for_each_entry(buf, &handle->buffers, list) { + if (strlen(name) == strlen(buf->name) && !strcmp(name, buf->name)) { + if (!buf->option) + break; + return buf->option->offset; + } + } + return 0; +} + int tracecmd_write_cmdlines(struct tracecmd_output *handle) { int ret; @@ -1446,6 +1512,37 @@ out: return ret; } +static int update_buffer_cpu_offset(struct tracecmd_output *handle, + const char *name, tsize_t offset) +{ + tsize_t b_offset; + tsize_t current; + + b_offset = get_buffer_file_offset(handle, name); + if (!b_offset) { + tracecmd_warning("Cannot find description for buffer %s\n", name); + return -1; + } + current = lseek64(handle->fd, 0, SEEK_CUR); + + /* Go to the option data, where will write the offest */ + if (lseek64(handle->fd, b_offset, SEEK_SET) == (off64_t)-1) { + tracecmd_warning("could not seek to %lld\n", b_offset); + return -1; + } + + if (do_write_check(handle, &offset, 8)) + return -1; + + /* Go back to end of file */ + if (lseek64(handle->fd, current, SEEK_SET) == (off64_t)-1) { + tracecmd_warning("could not seek to %lld\n", offset); + return -1; + } + return 0; +} + + static char *get_clock(struct tracecmd_output *handle) { struct tracefs_instance *inst; @@ -1465,17 +1562,14 @@ static char *get_clock(struct tracecmd_output *handle) return handle->trace_clock; } -int tracecmd_write_cpu_data(struct tracecmd_output *handle, - int cpus, char * const *cpu_data_files) +__hidden int out_write_cpu_data(struct tracecmd_output *handle, + int cpus, struct cpu_data_source *data, const char *buff_name) { - off64_t *offsets = NULL; - unsigned long long *sizes = NULL; - off64_t offset; + struct data_file_write *data_files = NULL; + tsize_t data_offs, offset; unsigned long long endian8; - char *clock = NULL; - off64_t check_size; - char *file; - struct stat st; + unsigned long long read_size; + char *clock; int ret; int i; @@ -1490,97 +1584,133 @@ int tracecmd_write_cpu_data(struct tracecmd_output *handle, goto out_free; } + data_offs = lseek64(handle->fd, 0, SEEK_CUR); if (do_write_check(handle, "flyrecord", 10)) goto out_free; - offsets = malloc(sizeof(*offsets) * cpus); - if (!offsets) - goto out_free; - sizes = malloc(sizeof(*sizes) * cpus); - if (!sizes) - goto out_free; - - offset = lseek64(handle->fd, 0, SEEK_CUR); - - /* hold any extra data for data */ - offset += cpus * (16); - - /* - * Unfortunately, the trace_clock data was placed after the - * cpu data, and wasn't accounted for with the offsets. - * We need to save room for the trace_clock file. This means - * we need to find the size of it before we define the final - * offsets. - */ - clock = get_clock(handle); - if (!clock) + data_files = calloc(cpus, sizeof(struct data_file_write)); + if (!data_files) goto out_free; - /* Save room for storing the size */ - offset += 8; - offset += strlen(clock); - /* 2 bytes for [] around the clock */ - offset += 2; - - /* Page align offset */ - offset = (offset + (handle->page_size - 1)) & ~(handle->page_size - 1); for (i = 0; i < cpus; i++) { - file = cpu_data_files[i]; - ret = stat(file, &st); - if (ret < 0) { - tracecmd_warning("can not stat '%s'", file); - goto out_free; - } - offsets[i] = offset; - sizes[i] = st.st_size; - offset += st.st_size; - offset = (offset + (handle->page_size - 1)) & ~(handle->page_size - 1); - - endian8 = convert_endian_8(handle, offsets[i]); + data_files[i].file_size = data[i].size; + /* Write 0 for trace data offset and size and store offsets of these fields */ + endian8 = 0; + data_files[i].doffset = lseek64(handle->fd, 0, SEEK_CUR); if (do_write_check(handle, &endian8, 8)) goto out_free; - endian8 = convert_endian_8(handle, sizes[i]); + data_files[i].soffset = lseek64(handle->fd, 0, SEEK_CUR); if (do_write_check(handle, &endian8, 8)) goto out_free; } - if (save_clock(handle, clock)) + update_buffer_cpu_offset(handle, buff_name, data_offs); + clock = get_clock(handle); + if (clock && save_clock(handle, clock)) goto out_free; - for (i = 0; i < cpus; i++) { + data_files[i].data_offset = lseek64(handle->fd, 0, SEEK_CUR); + /* Page align offset */ + data_files[i].data_offset = (data_files[i].data_offset + (handle->page_size - 1)) & ~(handle->page_size - 1); + data_files[i].data_offset = lseek64(handle->fd, data_files[i].data_offset, SEEK_SET); + if (data_files[i].data_offset == (off64_t)-1) + goto out_free; if (!tracecmd_get_quiet(handle)) fprintf(stderr, "CPU%d data recorded at offset=0x%llx\n", - i, (unsigned long long) offsets[i]); - offset = lseek64(handle->fd, offsets[i], SEEK_SET); - if (offset == (off64_t)-1) { - tracecmd_warning("could not seek to %lld\n", offsets[i]); + i, (unsigned long long) data_files[i].data_offset); + if (lseek64(data[i].fd, data[i].offset, SEEK_SET) == (off64_t)-1) goto out_free; + if (data[i].size) { + read_size = copy_file_fd(handle, data[i].fd); + if (read_size != data_files[i].file_size) { + errno = EINVAL; + tracecmd_warning("did not match size of %lld to %lld", + read_size, data_files[i].file_size); + goto out_free; + } + data_files[i].write_size = read_size; + } else { + data_files[i].write_size = 0; } - check_size = copy_file(handle, cpu_data_files[i]); - if (check_size != sizes[i]) { - errno = EINVAL; - tracecmd_warning("did not match size of %lld to %lld", - check_size, sizes[i]); + + /* Write the real CPU data offset in the file */ + if (lseek64(handle->fd, data_files[i].doffset, SEEK_SET) == (off64_t)-1) + goto out_free; + endian8 = convert_endian_8(handle, data_files[i].data_offset); + if (do_write_check(handle, &endian8, 8)) + goto out_free; + /* Write the real CPU data size in the file */ + if (lseek64(handle->fd, data_files[i].soffset, SEEK_SET) == (off64_t)-1) + goto out_free; + endian8 = convert_endian_8(handle, data_files[i].write_size); + if (do_write_check(handle, &endian8, 8)) + goto out_free; + offset = data_files[i].data_offset + data_files[i].write_size; + if (lseek64(handle->fd, offset, SEEK_SET) == (off64_t)-1) goto out_free; - } if (!tracecmd_get_quiet(handle)) fprintf(stderr, " %llu bytes in size\n", - (unsigned long long)check_size); + (unsigned long long)data_files[i].write_size); } - free(offsets); - free(sizes); + if (lseek64(handle->fd, 0, SEEK_END) == (off64_t)-1) + goto out_free; + free(data_files); handle->file_state = TRACECMD_FILE_CPU_FLYRECORD; return 0; out_free: - free(offsets); - free(sizes); + lseek64(handle->fd, 0, SEEK_END); + free(data_files); return -1; } +int tracecmd_write_cpu_data(struct tracecmd_output *handle, + int cpus, char * const *cpu_data_files, const char *buff_name) +{ + struct cpu_data_source *data; + struct stat st; + int size = 0; + int ret; + int i; + + data = calloc(cpus, sizeof(struct cpu_data_source)); + if (!data) + return -1; + for (i = 0; i < cpus; i++) + data[i].fd = -1; + for (i = 0; i < cpus; i++) { + ret = stat(cpu_data_files[i], &st); + if (ret < 0) { + tracecmd_warning("can not stat '%s'", cpu_data_files[i]); + break; + } + data[i].fd = open(cpu_data_files[i], O_RDONLY); + if (data[i].fd < 0) { + tracecmd_warning("Can't read '%s'", data[i].fd); + break; + } + + data[i].size = st.st_size; + data[i].offset = 0; + size += st.st_size; + } + + if (i < cpus) + ret = -1; + else + ret = out_write_cpu_data(handle, cpus, data, buff_name); + + for (i = 0; i < cpus; i++) { + if (data[i].fd >= 0) + close(data[i].fd); + } + free(data); + return ret; +} + int tracecmd_append_cpu_data(struct tracecmd_output *handle, int cpus, char * const *cpu_data_files) { @@ -1589,41 +1719,20 @@ int tracecmd_append_cpu_data(struct tracecmd_output *handle, ret = tracecmd_write_cpus(handle, cpus); if (ret) return ret; - + ret = tracecmd_write_buffer_info(handle); + if (ret) + return ret; ret = tracecmd_write_options(handle); if (ret) return ret; - return tracecmd_write_cpu_data(handle, cpus, cpu_data_files); + return tracecmd_write_cpu_data(handle, cpus, cpu_data_files, ""); } int tracecmd_append_buffer_cpu_data(struct tracecmd_output *handle, - struct tracecmd_option *option, - int cpus, char * const *cpu_data_files) + const char *name, int cpus, char * const *cpu_data_files) { - tsize_t offset; - stsize_t ret; - - offset = lseek64(handle->fd, 0, SEEK_CUR); - - /* Go to the option data, where will write the offest */ - ret = lseek64(handle->fd, option->offset, SEEK_SET); - if (ret == (off64_t)-1) { - tracecmd_warning("could not seek to %lld\n", option->offset); - return -1; - } - - if (do_write_check(handle, &offset, 8)) - return -1; - - /* Go back to end of file */ - ret = lseek64(handle->fd, offset, SEEK_SET); - if (ret == (off64_t)-1) { - tracecmd_warning("could not seek to %lld\n", offset); - return -1; - } - - return tracecmd_write_cpu_data(handle, cpus, cpu_data_files); + return tracecmd_write_cpu_data(handle, cpus, cpu_data_files, name); } struct tracecmd_output *tracecmd_get_output_handle_fd(int fd) @@ -1667,6 +1776,7 @@ struct tracecmd_output *tracecmd_get_output_handle_fd(int fd) handle->file_version = tracecmd_get_in_file_version(ihandle); handle->options_start = tracecmd_get_options_offset(ihandle); list_head_init(&handle->options); + list_head_init(&handle->buffers); tracecmd_close(ihandle); diff --git a/tracecmd/trace-listen.c b/tracecmd/trace-listen.c index 0cb70b7d..d812145b 100644 --- a/tracecmd/trace-listen.c +++ b/tracecmd/trace-listen.c @@ -610,7 +610,7 @@ static int put_together_file(int cpus, int ofd, const char *node, if (ret) goto out; } - ret = tracecmd_write_cpu_data(handle, cpus, temp_files); + ret = tracecmd_write_cpu_data(handle, cpus, temp_files, ""); out: tracecmd_output_close(handle); diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index 15e07cf0..78b0f5ec 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -4169,7 +4169,6 @@ static void touch_file(const char *file) } static void append_buffer(struct tracecmd_output *handle, - struct tracecmd_option *buffer_option, struct buffer_instance *instance, char **temp_files) { @@ -4197,7 +4196,7 @@ static void append_buffer(struct tracecmd_output *handle, touch_file(temp_files[i]); } - tracecmd_append_buffer_cpu_data(handle, buffer_option, + tracecmd_append_buffer_cpu_data(handle, tracefs_instance_get_name(instance->tracefs), cpu_count, temp_files); for (i = 0; i < instance->cpu_count; i++) { @@ -4447,7 +4446,7 @@ static void write_guest_file(struct buffer_instance *instance) die("failed to allocate memory"); } - if (tracecmd_write_cpu_data(handle, cpu_count, temp_files) < 0) + if (tracecmd_write_cpu_data(handle, cpu_count, temp_files, "") < 0) die("failed to write CPU data"); tracecmd_output_close(handle); @@ -4482,7 +4481,6 @@ error: static void record_data(struct common_record_context *ctx) { - struct tracecmd_option **buffer_options; struct tracecmd_output *handle; struct buffer_instance *instance; bool local = false; @@ -4551,9 +4549,6 @@ static void record_data(struct common_record_context *ctx) } if (buffers) { - buffer_options = malloc(sizeof(*buffer_options) * buffers); - if (!buffer_options) - die("Failed to allocate buffer options"); i = 0; for_each_instance(instance) { int cpus = instance->cpu_count != local_cpu_count ? @@ -4561,10 +4556,9 @@ static void record_data(struct common_record_context *ctx) if (instance->msg_handle) continue; - - buffer_options[i++] = tracecmd_add_buffer_option(handle, - tracefs_instance_get_name(instance->tracefs), - cpus); + tracecmd_add_buffer_info(handle, + tracefs_instance_get_name(instance->tracefs), + cpus); add_buffer_stat(handle, instance); } } @@ -4599,7 +4593,7 @@ static void record_data(struct common_record_context *ctx) if (instance->msg_handle) continue; print_stat(instance); - append_buffer(handle, buffer_options[i++], instance, temp_files); + append_buffer(handle, instance, temp_files); } } From patchwork Fri Sep 10 13:47:57 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: 12485185 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 18492C433FE for ; Fri, 10 Sep 2021 13:48:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EF589611CC for ; Fri, 10 Sep 2021 13:48:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233783AbhIJNtf (ORCPT ); Fri, 10 Sep 2021 09:49:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233582AbhIJNte (ORCPT ); Fri, 10 Sep 2021 09:49:34 -0400 Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB048C061574 for ; Fri, 10 Sep 2021 06:48:23 -0700 (PDT) Received: by mail-ed1-x531.google.com with SMTP id s25so2639488edw.0 for ; Fri, 10 Sep 2021 06:48:23 -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=rgKSGpz75JPloOaC/1DcOk+gg+XSb9yeocHeU0wjVV0=; b=Bu18nqmn7KqMt9RT+JL1afVPqpUmApc4TYP2o7fjgZJ3KMYggyTTwMXsJgFfNsmWjz +pMB+slQfJ3KwJw61QDRBOhuEqGTv5GPGRDUUmHD7UV7cnmX8xX6CWnG25YAIukfW12o 5yar/bPJ1xjXiK+infhjdQC6XHmBW7gk9PxLpNhjdY8E11CfRS+L5GAX12soVcGoPu5G zmWPb2VleWgUqVjliiDix8pz7xyZKPM/A142msOybgCDYKSfuv0H8glMk+j1rEL32gjO Xl54QZ2YTEXSdY5nYQ7HyH56F3tJLtcrmigtsNRQ9uDNAERzbtSSD9VyoiMWRuhwqTyx kRpg== 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=rgKSGpz75JPloOaC/1DcOk+gg+XSb9yeocHeU0wjVV0=; b=z9nChthAUgJk96TZHF7qgSX7PKiwEKb5UrD9UfwPAqELafcxNDpCfiT9C4Z/TxfM9V Whn5XwTqkZU2eM0BLkzA5V4lfHxvMrHdbvl9s+fe/FWfhAOfkA7L17bv0U6VNgg52xs5 bVJhbBrK4/Qnr2xYu4F/iEetWUXBfqleylVum9iULz1AynyDG6RO/B0B9PASuH6MVImI Wqjt9WgyVpM7PjFap15rTssitvVxY8PkmMB52m42V55GiUYCRWyg+zoNxQRRun2qq4eX VbE+ugVPAvqR9qUifEvkuw1GajpYCmzb7x6A2J4evbGSVcPajLyfZfzNYXZixp807XMy bMNw== X-Gm-Message-State: AOAM532MSw6Gtg28PhY5OJ5FXo7l8LRgIHxH6m13zWpL/mfKQaUtABop 1qGGXawImiJXG+S2sLbdUSHjcdSL+f4= X-Google-Smtp-Source: ABdhPJy8HZjkVvkS+auJ6POQYSUUkAfQMi2/dD4OiOZYfV4UvkqWEKefubOBOgGMwvGSW0qXlsPQZg== X-Received: by 2002:aa7:c782:: with SMTP id n2mr9262699eds.77.1631281702422; Fri, 10 Sep 2021 06:48:22 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r2sm605141edo.59.2021.09.10.06.48.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 06:48:21 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH 21/21] trace-cmd report: Init the top trace instance earlier Date: Fri, 10 Sep 2021 16:47:57 +0300 Message-Id: <20210910134757.2863982-22-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910134757.2863982-1-tz.stoyanov@gmail.com> References: <20210910134757.2863982-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Moved initialization of the instance before call to the tracecmd_cpus() API. This change is required for the upcoming trace file changes, where CPU count is set when the trace data are initialized. Signed-off-by: Tzvetomir Stoyanov (VMware) --- tracecmd/trace-read.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tracecmd/trace-read.c b/tracecmd/trace-read.c index 02f75cb9..f7ffb89e 100644 --- a/tracecmd/trace-read.c +++ b/tracecmd/trace-read.c @@ -1215,6 +1215,11 @@ static void read_data_info(struct list_head *handle_list, enum output_type otype list_for_each_entry(handles, handle_list, list) { int cpus; + if (!tracecmd_is_buffer_instance(handles->handle)) { + ret = tracecmd_init_data(handles->handle); + if (ret < 0) + die("failed to init data"); + } cpus = tracecmd_cpus(handles->handle); handles->cpus = cpus; handles->last_timestamp = calloc(cpus, sizeof(*handles->last_timestamp)); @@ -1225,9 +1230,6 @@ static void read_data_info(struct list_head *handle_list, enum output_type otype if (tracecmd_is_buffer_instance(handles->handle)) continue; - ret = tracecmd_init_data(handles->handle); - if (ret < 0) - die("failed to init data"); if (align_ts) { ts = tracecmd_get_first_ts(handles->handle); if (first || first_ts > ts)