From patchwork Tue Sep 14 13:12:12 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: 12493185 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 6FAD2C433EF for ; Tue, 14 Sep 2021 13:12:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B96DF610A6 for ; Tue, 14 Sep 2021 13:12:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233077AbhINNNz (ORCPT ); Tue, 14 Sep 2021 09:13:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232996AbhINNNz (ORCPT ); Tue, 14 Sep 2021 09:13:55 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6F7AC061574 for ; Tue, 14 Sep 2021 06:12:37 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id q11so20115617wrr.9 for ; Tue, 14 Sep 2021 06:12:37 -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=Ie0NkJ81tx5CrTz2YhnVLDpJ2QWzD4L0QZXwvBeTKZjPRzfy1ux4FWWIJNU9Tx3b0l K6ACSXsANI1fPpBfRUluZo6ky800JS2wMVvTNVYj/5+62J5fwQ7uD5ZcGTwGyrG3r1UA FRWwAoxrR0ORtp7Xf0K9wYyC4XYOSBQVe79xx9tmROo+fYvz1W6t6yG/XwI5PivTBjQx S/Y+BbwHh+6Dbb5TFu8L3wG4amKNF9fJnAmwEOLpcdNnPZ5buYKtXjUGlwWYOHdwkAyo 3dxXiKfV/vgdHPVFtnPBSUhpcfkx1uMoR4rY4B+FhsUwNuuFYWOXBTcwCaUBgiRX9dje wIfw== 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=o5Ly4G0bzNgRml8WgF0+vGEg2rl1Gi7F0w9UKMOyUPywf3khsLeClFtV4WE+vitx19 gmSOiADF198GaU+fR+D7i4EddheDeNFhaBkfbO7Q8jvwkQtwMyP0WfIwyKExDE+LvRqy mEqujFWf/ivTE0UBuwCLAWsMmBkx68XBAbQA4S8FUqWb58CABiATZhnhQ6lZqba/P88N BIp5UvRle1EqbBuLtp6DLCq9kcWk6dL1f1Eyq8E/D5su5q+lqnXXwY3K24VM+RgBVD+a wR0+oSIrIfn0u/B4/AscfqrGG2TNF2uA6nKRcIJKd1OC+H1HeQrqtM6iiufgdfvLB0aM 6wmQ== X-Gm-Message-State: AOAM532EouF0dee7DPEMpFJufev58kLY2nRgrXoRqTIkOgdNeP8RTlYX 9hCg6xF2oS44lDUiNauMDG07Kwq+Bl8= X-Google-Smtp-Source: ABdhPJwR9hJSSEQXlJd5tT7Q8tcmHpy6uKMRuLCyxbqEDL8/ZplX4ITquXS3BIlIaC0oTvjheWYqgQ== X-Received: by 2002:a5d:54cf:: with SMTP id x15mr19334850wrv.27.1631625156264; Tue, 14 Sep 2021 06:12:36 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.12.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:12:35 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 01/21] trace-cmd library: Read option id with correct endian Date: Tue, 14 Sep 2021 16:12:12 +0300 Message-Id: <20210914131232.3964615-2-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 Tue Sep 14 13:12:13 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: 12493189 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 7BA70C433FE for ; Tue, 14 Sep 2021 13:12:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5A4BB610CE for ; Tue, 14 Sep 2021 13:12:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233087AbhINNN4 (ORCPT ); Tue, 14 Sep 2021 09:13:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232996AbhINNN4 (ORCPT ); Tue, 14 Sep 2021 09:13:56 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE92CC061574 for ; Tue, 14 Sep 2021 06:12:38 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id b6so20123524wrh.10 for ; Tue, 14 Sep 2021 06:12:38 -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=OPvzY51obHJtQwvUDDHVLC10GRJ/141WN/VijQQoHQsO3rHYHw2HQvz//JdUXRvNU5 u57A30wUO3oV+NU21wP4TVeCeYikIWzBa33UvypWtpexDoarqNSVMcyyaCtJpGLtLeps xnyh/QdPiAxRucrkg3B+DOihuIG99C4OIi1XEu9iGD3O+hVDUsyNJamIgSt5dpFGPsO+ lC+NlO/mSSr5mMyy+hKYFDTaguFwu21CrckRKdl4zFLMPBG0hjdQtSUE9TVR2TkhPXUi LegQl7/1M4KEthOFGpSXGu3NX+ifWYTLeRS+tRAccjldS3HmKgUipLp5Fx/iLtHK5NQj Hg9A== 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=MEYm38CdO2Zu5zTF+ouRHglMs5/n46g6jg9axFF+nABzNMwtGoYcO2TdLqb5S6c3xy QIGvAODlf3oYi8sm7vrgDhnXQeUd1AHYS/6XjN+++LuY3eQ/6Y9Bp6lrMZJ4fTDfnUx2 MlNVBWWpgcpcIG8o6zQAh/NkpYY/EvbbxkUYoFN0BAih7apo26w16HGYWzvhAn5Cw0OG TNHm3F4zuMEe0E7EoQzXjdYMr2vWL/ddRFTIWd+GvZA3DVqp/jQ4QHh8njmA3coiJs2r 4bXIEtsTSx4mpxNUWSSZcu/kPeCW+vOWHTbUfNUdLBObKAvy7Evif9XP7kG42jydpP0l qKHw== X-Gm-Message-State: AOAM533H8pvHvRnKz7LE88R19JXNUZ2amdhX55CrhZPU8NYESR3wOkM6 +JnoTWY5B33GxhKPT1ROcIU= X-Google-Smtp-Source: ABdhPJzJQM6MT6y3Or5W1WG7aAmwoQk8yymm4K5HtfpZmj3ecDd0ZhstL7Hlfr7nM16RStvwr98rJA== X-Received: by 2002:adf:f789:: with SMTP id q9mr18757848wrp.367.1631625157314; Tue, 14 Sep 2021 06:12:37 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.12.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:12:36 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 02/21] trace-cmd report: Fix typos in error messages Date: Tue, 14 Sep 2021 16:12:13 +0300 Message-Id: <20210914131232.3964615-3-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 Tue Sep 14 13:12:14 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: 12493191 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 9BA75C4332F for ; Tue, 14 Sep 2021 13:12:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 768FE610D1 for ; Tue, 14 Sep 2021 13:12:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233093AbhINNN6 (ORCPT ); Tue, 14 Sep 2021 09:13:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232996AbhINNN5 (ORCPT ); Tue, 14 Sep 2021 09:13:57 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9EC1C061574 for ; Tue, 14 Sep 2021 06:12:39 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id t8so15106849wrq.4 for ; Tue, 14 Sep 2021 06:12:39 -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=cAhm5s/M3pGZ54Twi4Ir1E6KkHXe/h9hx0JSYNpyHO4S+z5gRtRS3Ajctibj8Mntz+ x0GLfqvm9U1VYh1NOb7g5MY1ihDEQZg1Ua5SGqDsUkGkuoKHUSArPT1tx5NoVPqCZ2eC GgCiEfW2YBElVdNr+tM/iPk3o5gxYNRWX/e3/4koepQ42gZbZEb5mzn8b2h+LODmhjd7 pdKsKeK+vKW+CoAlL7kJZtuCq6lHfYEKpZRRtDj7mIRr2YXn1DOd1/8FxQ6ZwU5JOL5c /8KoNfEd6Oxw2DKW+zvambRMHx7SKfWXJnLtaW6ylM8ht+UKQ8EeaEVULsnRn/Ti/fwY edSw== 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=WILwj5L1ns1hWFJmMGXhhbj555xJlJrSOFmtBCHtfWduahLkOdPT5vP5Lqk/c0JGyt c6TSMKjngytOohGCLuK+l/vzr4vkH6op32Bxy2oMB77rC4tiZJkkQkM5uttCNexxj1L4 EvbNMaTdaLdTsTNMga8wfzDV9Vuu4Pph2eJDQJ7jynoRQe2XiC3RylRyosrQi1jWfVk0 CRl0FhbKQTvSKQNoY8I/XVHfazJGbisLhUuOTlLuZdXnfhqHoiDx1Lc+8OS5RQRlUIG+ 3S0fUf+32f9+s4ZKPnrUUViVtozmk5aG57cWRaxk0fDJ2NdKdHw20upfreaYBjJqjfja 3z8g== X-Gm-Message-State: AOAM5309h3u76q7rxX6dFLMyIhjTb8mMGwYz9xyuNLc5RyuG9/wesLi1 dLnV3Tl63ubcrNNXDBxgVX98C1DxOH4= X-Google-Smtp-Source: ABdhPJzivPNAs6AEAPKPnvT1oEntDCpvZYZ45DlRqv12LYDhnt/tMtsKnpCnsXKTzU/vrmKrFIpWvA== X-Received: by 2002:a5d:560e:: with SMTP id l14mr18299291wrv.205.1631625158515; Tue, 14 Sep 2021 06:12:38 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.12.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:12:37 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 03/21] trace-cmd library: Fix version string memory leak Date: Tue, 14 Sep 2021 16:12:14 +0300 Message-Id: <20210914131232.3964615-4-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 Tue Sep 14 13:12:15 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: 12493187 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 9BA49C433EF for ; Tue, 14 Sep 2021 13:12:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7E884610CE for ; Tue, 14 Sep 2021 13:12:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233105AbhINNN7 (ORCPT ); Tue, 14 Sep 2021 09:13:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232996AbhINNN6 (ORCPT ); Tue, 14 Sep 2021 09:13:58 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 019EBC061574 for ; Tue, 14 Sep 2021 06:12:41 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id u19-20020a7bc053000000b002f8d045b2caso2513939wmc.1 for ; Tue, 14 Sep 2021 06:12:40 -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=k2b8JOCYsE5SJgylRZePDaErpp+VjrXFe8jwbuCkrFcDXGl5pUhH7eFeSJxfKA5bM1 IPxU4PNKdRUOqCT5hl/mF0oMLmo6db/+i3s42GRVJqdtnLMzl9+F5M70xoidmVkXZKu6 LiPkTic2IwKPah2QFcIyQH98D2vH+3HT17TXMUC3lEuwPMqHnr7c8AQPIDPOvKz3zn2O CQsMvxuBWWWM8Y167FXZYDs5LpTG35O9MPDb7aGbT08zqZ9F+dqelkp16PbpC5AZ8ZDz 3pK5jxU+JsssviW4dNWM/siGwqmiPm9Fi/MSBbM2mJKXCED1HXJxFYYxiu0jj+FMXSEO tWuQ== 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=YWO2snB4z2XGg2Pql9Eg9HcfoxX7I11WMAJO1Rkq3pp8nYzpNLmCxfM4ikF5E/SJy7 sR0iYUoin0+jMBRKu7BKvI8Iy7HzwysfR1OhrBzAhzm0EOm6aCfsfJ8LVO2Pyl/4sQjl Qyb/RzyDHvLfhUAeHwJ03s4y3Hhxd1r5oKt1OEVLfKbrRoMpCLnw2+8yJpneFQspBtcF u1JG4mHsOmPB3CamF3E+vmm/CVM4knvH3uqHiDwsRP5qTfbPFfao0SzI0YmYQjxjmowd zDSn7IJr1VfR98/3Po1/K4AklcdeAOSP1O4iXH1plXMw7LKNsFTYsHL9S4jSjELuRMJZ 2DTw== X-Gm-Message-State: AOAM531SJd/1OZFH01fXX24GWEZt3fTZNUuhrvHd123Q5hpLUYYyzr2z F7ytz1nZ6htZhyJ0xy5sze8ZN/rOYz4= X-Google-Smtp-Source: ABdhPJyhvD7GgxjzPmsvQ8W4XQYqST94/TtZZoqEyxKPf7ssd/tanYHO0pZX0ri1NRLHyfLMTm6v8Q== X-Received: by 2002:a1c:4d01:: with SMTP id o1mr2138872wmh.61.1631625159558; Tue, 14 Sep 2021 06:12:39 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.12.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:12:39 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 04/21] trace-cmd library: Fixed a memory leak on input handler close Date: Tue, 14 Sep 2021 16:12:15 +0300 Message-Id: <20210914131232.3964615-5-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 Tue Sep 14 13:12:16 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: 12493193 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 C518AC433F5 for ; Tue, 14 Sep 2021 13:12:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A57D1610D1 for ; Tue, 14 Sep 2021 13:12:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233113AbhINNOA (ORCPT ); Tue, 14 Sep 2021 09:14:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232996AbhINNN7 (ORCPT ); Tue, 14 Sep 2021 09:13:59 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48E20C061574 for ; Tue, 14 Sep 2021 06:12:42 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id l18-20020a05600c4f1200b002f8cf606262so2058620wmq.1 for ; Tue, 14 Sep 2021 06:12:42 -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=V9Y3FnQkwto649k6Haw/IZoJP8YREpA1zAcGSN46IwZfy8JTXUmhayM+SxbuyZjKDG DHYTy+EEunmY90N6Qp+HU1hgeHcfuaTI/u//GsvtkJiE99F7+MZBbqoKk419JDNJxD8G Hpcus81yIPT+LcEdet4vzpyo+4tlioYm5UoxBP639/x3iHiTD+Hj19//dGsnMZeaf9Pp 6R+FvtyPfGaSJStFyTBupf68dgDY04p+n88Oqz72xvK9SgINxqjb10jIQ9zQoqO+QiOr LsJUI5EdUMk7SWnonDGu6sCM5+PjULwxWIF24xmSjo8+/A2lihBnQcE43wK5NAYmqkW7 IUCw== 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=r4JFpD0/M4GFD0qPz+14ypice868cBoF3ZlyBz1ph0VM0Qd/PB/8FmwsmCkALT1CWY 3yO89hi3VnhlAp60rmfAKMYUSYvgFwVcjxoPkCvS2zupIX1Xw3UIclrdBkQRav4KM+gB EpuYmrj3ppN9+Cc+ainLothd+rD8ydCKbA2y7ypn2gZAYdQTfv05YDA6UE+HkBOz9DJK q35AEuXI0s9x7CdMOsfViHgqbcoR2KDpr69kSyJ0ve1Ei+v3SHviYiXLXWITUB3HNSzC kK/bfybTiLB/YBHJtFcBEgdJ/cJR0mKQPv/NfmYVItkbYvk40mQmbzkkVMyJ9yPoGCex uIjg== X-Gm-Message-State: AOAM5318xyCRxk1h0F+t2z9sQWilsbq6tTuVOA+XzEdFzpU8C4jDu8kB 1fj0plVRrirzaxYqNDFSm4atN/GmVHE= X-Google-Smtp-Source: ABdhPJy9Bs6OviwvLf5ogcQwSKX4fqIMbw0/JakowkPRyN7pbfbjrthBnc+tDkUYWHmtRRdZ8Laptw== X-Received: by 2002:a05:600c:3790:: with SMTP id o16mr2130105wmr.157.1631625160692; Tue, 14 Sep 2021 06:12:40 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.12.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:12:40 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 05/21] trace-cmd library: Do not pass guests list to a buffer instance Date: Tue, 14 Sep 2021 16:12:16 +0300 Message-Id: <20210914131232.3964615-6-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 Tue Sep 14 13:12:17 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: 12493195 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 AE097C433EF for ; Tue, 14 Sep 2021 13:12:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8A53A610E6 for ; Tue, 14 Sep 2021 13:12:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233122AbhINNOB (ORCPT ); Tue, 14 Sep 2021 09:14:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232996AbhINNOA (ORCPT ); Tue, 14 Sep 2021 09:14:00 -0400 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38DAAC061574 for ; Tue, 14 Sep 2021 06:12:43 -0700 (PDT) Received: by mail-wm1-x336.google.com with SMTP id e26so9242747wmk.2 for ; Tue, 14 Sep 2021 06:12:43 -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=aVKHGZamVeiLsY5FthUF4QQkjHOOkrjuEnZ2UTrfkBSSSiqjCVMhQV98y81ktEmW96 qBClURnDa9dByNg9U2Lil2a8caTTE6dIql5LHLi6/39b2zBQBXx9uadvXZYBSDC93yJN HYxN/Ip+22wQVuVH1XRqFfeRUKnd6kmrqjSuXeXGTlB5Cx3bLxTyPcTx5FUPxQTaXYpx kCwp0xVt7ne/9MsZa2/6ngHQEoAkE8x3W5wo/TvDhEZZ4AV7JPBvLfS02Am9HcBayJd0 T3o1r/sOvRLz2h5NwB5POci3lyuGSK6PhgeSqA+1amD9q3aIcje2ZXJmuCs81GaZg6Av 6Jpw== 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=gnjEP6l/Hm3TUac+F+L6LG1IZLLREt1EgwU/UUTqd2KFx9dU8KbJRSYlEDwIC9xRxn aVJAPuuLaTTKkNGIH1WSMbT8azuPii4pFYjkY8+QUCkyt9NTbhOYAvOSRt5NLh7/eYNS BBga7CBJBBJYyT/8QXBRNY8spqq5Yee2fEnT8FnEkvJNwgV7pqE4vf/aTKl9ev3uY8/P xC3GGOPHaEjOlmrO+SPQ+n1Vn0UJt0HY7yOiQMgq3ylh4hbvl+HdW24hlnqgLlPRYfsc tq6P+6iNATgSY6RCVrfB87arkYv4IoSYPfIGEBG3e5HLLetJClNuElsrSp7rEy69r9KX NnYw== X-Gm-Message-State: AOAM531Izb3Jxjbxn9UBlTXwhzZqGmAZRj8HPSk6EsR84ObtrHGH229X qLbwz4hdVDMp4XiazTrCoMlAoMtTRUU= X-Google-Smtp-Source: ABdhPJxCbk0ai5qpuspZriVjCWEOTw4IFUdChhX3hmIPg/uYfJ4DQFZEBPExA9fY2ku1kKjQtyBs6w== X-Received: by 2002:a05:600c:2193:: with SMTP id e19mr1772269wme.38.1631625161772; Tue, 14 Sep 2021 06:12:41 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.12.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:12:41 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 06/21] trace-cmd library: Set long size to the input tep handler when it is read from the file Date: Tue, 14 Sep 2021 16:12:17 +0300 Message-Id: <20210914131232.3964615-7-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 Tue Sep 14 13:12:18 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: 12493197 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 C5071C433FE for ; Tue, 14 Sep 2021 13:12:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A18E5610D1 for ; Tue, 14 Sep 2021 13:12:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233129AbhINNOC (ORCPT ); Tue, 14 Sep 2021 09:14:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232996AbhINNOB (ORCPT ); Tue, 14 Sep 2021 09:14:01 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F5A3C061574 for ; Tue, 14 Sep 2021 06:12:44 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id k5-20020a05600c1c8500b002f76c42214bso2045285wms.3 for ; Tue, 14 Sep 2021 06:12:44 -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=iHbl/58swxEP+2R4EUL7BmGjrykf7sAqFNSgdDuIakQ=; b=doqr6TRSwiVHgH658Wfr4RomKxpOLz3txqSsYxTMmSGGzxpvPyqFNVd3wb4eT/PDym p1A9L+P9vgFZHRDCwYBSwge9rPtaFBd5ewCFSsXvDCIzXiJZNnZecOWCQ2UTPMQHomGu 77d1rkwBkGyAgq1w8wmMkuNosUN1bqUOGIBMG74Dola8HYSn/ty9gYQV5FHUXIU3lruS T2r36stBzC+MXw0cixYFTeXlthz/yUHwSEIxnB3pGxUip5Q3gkZKO1Ah9z6iSeOxTbrL iJVsqn4fX5B/hppdrvpliHon9CFmNKycFyAfKHMOYylgJ1JNdoP7OSSIZ+3stwOQUJKC I+9w== 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=iHbl/58swxEP+2R4EUL7BmGjrykf7sAqFNSgdDuIakQ=; b=GLViIEgv3/CUIvTxIjV6HvR7NPU8/3KrqVcr38bORHSro5B1FfGF9iN3Evg6MQloJ0 kr47Nrw7qvaxYgg4WPbwd7tPgY8kQQjgXf5zf7azi9Uv4/lAQy2qxPs02EwdvCJ0cv6S EXsmEMvcyPwMmg49zLoQx98KhQZFOGI/prs2JZMNCTYYsqluhFj5ZWfYZRhHJhv1fKin EFmCRtXRpTVXp3EINf7rdAsd0AinHGpDO31ENBMLubAyKrkAfvHOXgOFgluo9z6mntS4 RVNBu+3bNUALvy1streG/OkEyOm1AEEKILfMpm4O3UtLmaoMil4bW37vYC16AFAekpd7 bvyw== X-Gm-Message-State: AOAM532t+gBjPWJBz87PtKlAxRgDFIFuna6yQnr97NA1TlRkjxOWTZiV HCH9mVKz9O60yHlMSJEfm87d23rhpz8= X-Google-Smtp-Source: ABdhPJzya+kmjW98fi3HWPT04KfeoUfG9Hg0hhgAmOf9amIEPLzIiAD9WvEVScu4ui0yxyjBm7psXQ== X-Received: by 2002:a7b:c767:: with SMTP id x7mr2207554wmk.62.1631625162919; Tue, 14 Sep 2021 06:12:42 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.12.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:12:42 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 07/21] trace-cmd library: Do not use local variables when reading CPU stat option Date: Tue, 14 Sep 2021 16:12:18 +0300 Message-Id: <20210914131232.3964615-8-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 | 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 3d7f1c48..d020cfae 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 */ @@ -2813,8 +2817,6 @@ static int handle_options(struct tracecmd_input *handle) } - handle->cpustats = cpustats; - return 0; } From patchwork Tue Sep 14 13:12:19 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: 12493199 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 CDB1CC433F5 for ; Tue, 14 Sep 2021 13:12:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B6243610D1 for ; Tue, 14 Sep 2021 13:12:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233132AbhINNOD (ORCPT ); Tue, 14 Sep 2021 09:14:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232996AbhINNOC (ORCPT ); Tue, 14 Sep 2021 09:14:02 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D822C061574 for ; Tue, 14 Sep 2021 06:12:45 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id y132so9230405wmc.1 for ; Tue, 14 Sep 2021 06:12:45 -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=/iP4a8bfrh1K3oUHJHD9zBIH/f3RsH2LCFQaSP0YOGI=; b=DZIYpPXiE3KC/BeNK0Z7JBNQoZfj6fqndLcdjCMqrFKuG4xvWRSM0LktKW9CzrRywS 1pwAUsIrCXNt6DTJMDXRVHsCqVncsl1/1QogAdVIOyZxzwzcw4aat4gJ/mFpotMSJuE2 Ij+ewIa0yzHNh76OJXzG6ycLHxnMt2aoHF6huK08fx3LSbVk7RoAZOnBw/0cVVFcWaDs FgsPwDvirFqIEZHc7hAAsSUE7SPAyftT9LLHxxE3HJuvtCoCMlATdnBgIHBlUNa/vd4J 4XP2/qxXqW9uZMQyN9jU2i97MrMxRt50tD9gKX9SEzIKd/9Q4Y0hQ9byy35AVLbD3mGW qLTQ== 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=/iP4a8bfrh1K3oUHJHD9zBIH/f3RsH2LCFQaSP0YOGI=; b=XNiNfX6Y85kAb1UZA7VGgxf9rVsSPCmn/bBvH6BmVO5Rr+vK0RU8h5s1rHjotWDgWR C6RyA40E/M9O84FtoEGREHBdO7iIptEJG5mk3IiLLzrxjH9iVjcdL5Y86Oz3OnD8bFG0 amaTPhGLoSQp5/rXJhlAl5Sw0rpjBpXlc1jRlBN+zf97AmkaFOvm8rmpBPHy6sXAgxKx 6gaWMHNfBJco4gSlY9yORXjd70B9D9cr3h+jxbG8EVvSbpD6SFyZxWirnmoqupcFVT/x z9RbXFnwMTpQC9KIqvZKgnLU6PKuDbaxXUWQec1VrDyGbqwywl7JH/eqM9TjHZIqHIqm oXpA== X-Gm-Message-State: AOAM530rHzanxo+IQLGqk0Bc8JmfeI30/v84eGZs7YT8+U5ehYI45ELd iwSMnM+SLOmsf3zkgrYBDDRkwhFasmw= X-Google-Smtp-Source: ABdhPJxucop80mnCrEbjvS60NtUKN3mPTDl7JjUm9hNbf91wUu0DKdpD4vCHD//ju8JjnW9ib2WTbA== X-Received: by 2002:a1c:ed03:: with SMTP id l3mr2230993wmh.56.1631625163917; Tue, 14 Sep 2021 06:12:43 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.12.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:12:43 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 08/21] trace-cmd library: Reuse within the library the function that checks file state. Date: Tue, 14 Sep 2021 16:12:19 +0300 Message-Id: <20210914131232.3964615-9-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 d020cfae..bd0517e1 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -4222,3 +4222,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 Tue Sep 14 13:12:20 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: 12493201 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 15F3FC433FE for ; Tue, 14 Sep 2021 13:12:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0019B610D1 for ; Tue, 14 Sep 2021 13:12:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232996AbhINNOE (ORCPT ); Tue, 14 Sep 2021 09:14:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233135AbhINNOD (ORCPT ); Tue, 14 Sep 2021 09:14:03 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7275CC061574 for ; Tue, 14 Sep 2021 06:12:46 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id t18so20187547wrb.0 for ; Tue, 14 Sep 2021 06:12:46 -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=3OTYZuCAo5R2kjph4kWPglVUSjijOi7+4l6GtPJgV2w=; b=F/oDdRmIjSQ0IkMQvvfeUFBG+xnUOmM7OzXSEZPpWHBzJRio/TDypfWn7y+58nZc4k VFheDFPXSDVngimGTXPr+ZA4Siy0P0p+HC/jbSgD0H2XfnhtPQXQ0Nl///GllBd2Tk8L BKfsQUW+I1bAAXPMMqlL1LqzMkT/+/y8xT3jK6bNZBVhxzT0R9miYK2CfCB3WMzaYzZj Y5woCyGP+cSoauHSg3wIYXxSIj8aNdvYtxN9nmAgm8LgvlhvYqLUi4HQRiqV/BgzRYjV 3I48IPfEQsZGwqgoW/nN81hJi2+jGKzzYkZ06lLXi3YQu8lp5kaBYPOOCkQhfQnqmwRz uQnw== 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=3OTYZuCAo5R2kjph4kWPglVUSjijOi7+4l6GtPJgV2w=; b=jyvCDgE0JjC6j4itt0FmEn/hbAxATC7IcJ8O7aNKYs/yYcO5G1aujk0JhL/+JLrAfX E+KTf46YVLWP3kyAZoKq1FnE4ruJZpZo1Y54CT7tKZ6kiTwQ7lxzAfSHBOthHyDv2+IZ +nrR+f1x2od9zuxKaVOfkfBHjSAJQ1fgdraGq0V6Zko5U2VcMZxn56joT/rduxxFNmhM UsGCGyUp3Q9yt39BaunW5GkDf49sB+WMgGkbNFuxrT5iHyXgc2J3qYukMVwgzyw4cnnc 4bV1zJpjK8OX0QfTUEp4aoz8V+7w42BcpdzFlhdKD5jr03G/mFoFXNMwQYY7Ixud5Pqv k0NQ== X-Gm-Message-State: AOAM532QGFhSsnUVJ7uRYUYBQNQVkbsaJs/wyp0W36kfxneKoDanATg2 josQAkp64b2T/dBNUicWvQ3CHx8i52k= X-Google-Smtp-Source: ABdhPJx/wXhCzLrIJkspymiK87dfozkVOjtl4StU9lHbuftfBVC/9v4CD7Q9SOILceB4LOLv7inMJQ== X-Received: by 2002:a5d:5258:: with SMTP id k24mr18842493wrc.390.1631625165061; Tue, 14 Sep 2021 06:12:45 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.12.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:12:44 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 09/21] trace-cmd library: Fix possible memory leak in read_ftrace_files() Date: Tue, 14 Sep 2021 16:12:20 +0300 Message-Id: <20210914131232.3964615-10-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 bd0517e1..94fd8693 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 Tue Sep 14 13:12:21 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: 12493203 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 EAA1BC4332F for ; Tue, 14 Sep 2021 13:12:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D2B91610CE for ; Tue, 14 Sep 2021 13:12:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233142AbhINNOF (ORCPT ); Tue, 14 Sep 2021 09:14:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233135AbhINNOF (ORCPT ); Tue, 14 Sep 2021 09:14:05 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B4BEEC061574 for ; Tue, 14 Sep 2021 06:12:47 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id w29so19367095wra.8 for ; Tue, 14 Sep 2021 06:12:47 -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=0j1dgOlKG6MfWbaTr4bXRbYYtvn+fCKUl29MSaEewTs=; b=DP5JYN+wgFEUacgL0VmMAqJqLPY3okHKBfKHuhUj1cPFg4e7oMUNxE8XCJU+aU5lNo vKKj8sMMmVqPVoYMdfEo1KT4Q2tIVBo7q7E3qG+duRhvsqb+4LG8cTvrN+IlXX+kmBCb Wyv2eb6FGO1RU51W6QOZLHCjnvLIuZgSp7/XAnN0Lo46pu1FG+XsCojdJGPvOHf5URPU IRyf//a7FEgoYgANuO8DqyfozlMOG/8rlBu/0UhEGrq5EtfAkz01wgQqGiyCZ2r3nR0u iiTWwmGrQcHZTuRvidDuw6DvKHjun2cM9wDTdQO/yDV3V13iN2mUleTj92lROR6lMjsD 2+gg== 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=0j1dgOlKG6MfWbaTr4bXRbYYtvn+fCKUl29MSaEewTs=; b=M6wikIGlFRgWZMIR7MYLXK2emBdnr57DTAQ3oWX/D0W13zWgWrmhVv+CiNN1vOKc/B ZxQ2dLFT9XLDn3exH1AxgE8z1/zXww81STJnf62YEwLzfBMKJ8T8FZKx/YJxzh5leGvL /MYGBg0HSiVNcr9YRJOTKDbyM4Xgt7TzFxbMrU65qkSlfcZar5I9vpioPtl6RDpwuKh/ XzVy3VVNowgWL+ktyA/W6cdvMYp4OYE3qgNErnp23xyUOFa91UNQperuS5DnvAJcfL2I DHjne8fKVnbWOULI3b2H84PKM0EP/P4O4TZjQnIcK9z6BOVIJekIM9E4u/wW6p1S9HRa s2xg== X-Gm-Message-State: AOAM5305w9OneHLvLgmjCRjX5tWtWCD4bxsYedgyIOfTUcmhKmze52nH LthGP5DYSMZKaZP2ePhUo7To2UCwm+E= X-Google-Smtp-Source: ABdhPJwJkWUDAn6ahNWWajqrc8/ObSNvh4egYLYmp4LK95HAszhgbdVq3eAvv/jvUobyX0bs4tJAdQ== X-Received: by 2002:a5d:404b:: with SMTP id w11mr18876796wrp.437.1631625166278; Tue, 14 Sep 2021 06:12:46 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.12.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:12:45 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 10/21] trace-cmd library: Fix possible memory leak in read_event_files() Date: Tue, 14 Sep 2021 16:12:21 +0300 Message-Id: <20210914131232.3964615-11-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 94fd8693..32358ce9 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 Tue Sep 14 13:12:22 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: 12493205 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 12DDAC433EF for ; Tue, 14 Sep 2021 13:12:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F10F5610D1 for ; Tue, 14 Sep 2021 13:12:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233143AbhINNOG (ORCPT ); Tue, 14 Sep 2021 09:14:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233135AbhINNOG (ORCPT ); Tue, 14 Sep 2021 09:14:06 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF1F7C061574 for ; Tue, 14 Sep 2021 06:12:48 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id d6so20125851wrc.11 for ; Tue, 14 Sep 2021 06:12:48 -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=hIaSg/ThuS+rjVyXJEp0CTM6VFI1pB32pj1o7a+D8Jo=; b=eyr+aMPR9P7AaYiahN3Ov9qEvHgNTuHpHedl07Rpq76sjwtK0952HtPkHpjni5bsr+ vvyfORR5JE0gytSLsi5z0lAFQAdZ5AbX9Ufh22bNetJ/f8/TsZskQAlmL8jxhsOoVJky NRJL9ltIdl+ZY6o/eiLNQuBUoJYc3wpyb0Ap0Kxicxgp6jdvx6+9CBYMPDlP2d/wO+BS fq0mb2ClspqrZPku4x2ep5k8z5VXyZwu+u7SnDeFNx/qVfS07LYyvV6Pzx5KEGG/7S6e 7CjvW26GiWurkJ1+rw7iuYyj815YxcFCcGSu3AcJ6/EXanK43ogZ9nqbQxIm5jdXsuHF 1gpw== 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=hIaSg/ThuS+rjVyXJEp0CTM6VFI1pB32pj1o7a+D8Jo=; b=gJ/3gkdCkoxJwCSi8k6O0xs+4Uv5+Xsp354TdVJ6NHJB6vSfGPk0RgeZrchQZPCYEo VC6Ge1ifv/5mazVdb6oLD9YV0X8PNhLDaA9uy3v96vG9l3i1H6l96rPdgUHDT2CSUm7j Oit6Z0Ldv9oWjo02r18e9KykGZks/v4rIXh07Hp/PYWcdok007L6wvkLFWZ+GqX/yAZe TOWSo9rdiZr/6VqV2eWWEO6qvo54KJJmFzlL2Vs2ZlJwRZS7eHP6NGSVUkTdEc48KNso FDu55wejxF/fWGwFfdTEIOOCU7/ecYGRFVC0gryYDzynfP4oxD/hcaDYXiUNnx+Z4KbS QBHQ== X-Gm-Message-State: AOAM5300Drtttc0tEL06aBprVd6Y0WEzg3bPSqGOwAGRpVzNYAUEtFnV 82VWH27OmzhPIoaIqU8PL3pkwG0NSSA= X-Google-Smtp-Source: ABdhPJwvg1lYaU+1+8ybAnF/iA9nJmxnpvJJ4Fqps/GSIPMj0vcuOtLhU3c3bSAf9qzvSUubiFCpZQ== X-Received: by 2002:adf:f6c7:: with SMTP id y7mr7132108wrp.44.1631625167426; Tue, 14 Sep 2021 06:12:47 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.12.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:12:46 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 11/21] trace-cmd library: Fix possible memory leak in read_proc_kallsyms() Date: Tue, 14 Sep 2021 16:12:22 +0300 Message-Id: <20210914131232.3964615-12-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 32358ce9..9b23063e 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 Tue Sep 14 13:12:23 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: 12493207 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 CACFFC433F5 for ; Tue, 14 Sep 2021 13:12:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AE3EE610D1 for ; Tue, 14 Sep 2021 13:12:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233145AbhINNOH (ORCPT ); Tue, 14 Sep 2021 09:14:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233135AbhINNOH (ORCPT ); Tue, 14 Sep 2021 09:14:07 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D55DDC061574 for ; Tue, 14 Sep 2021 06:12:49 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id 140so5813004wma.0 for ; Tue, 14 Sep 2021 06:12:49 -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=WIhyREvMq7bERJU6rUAI/8YHOuVEpU7iUhUrHTuHcqg=; b=KR3hCJQN2tFcV1Nv7cPMJxnkXu/z/maJumAbU6SLji2q+815uthvsfFdCkW1be8UWG opCgh8hkTUwkeH7pym1mJrlFBblNSA17kIanInHr9FgJ0CoqGPHKU5qO1Js+NJ9uqXc7 ikZS1p+tIXZt1gvffApZ4+zqRrL3pG0VLDCMgZRvL5VCijdglSOlbKv9KPaI4C+OUDxB QSnYwTh8WebJa3fmE8/CDgwjVBiuVfoKCp2UrfhCVnQsHlAl4Ups74rN01RwdpZDgb+P LkqnII2IB0I8N7uMRY2vfkckiBeeOWzsWhahur3Ja5LryFxxXehChFLfynZVGxJNsG48 G7jg== 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=WIhyREvMq7bERJU6rUAI/8YHOuVEpU7iUhUrHTuHcqg=; b=sOw05uIwk8Psrv5D+kPt3ztoIj1mscrkEX6K0oDKaz/x5pRU+asqici69ze2gLeywo jNCKdJ63fZpOsAMEep423HOiuAdJhCrM0DQejAzqH/qbES8U9xx0+DgT3D5c9VIZ4zvW CqOBb0lE7locVfpBklGBx2GuUkNGK1QP3c5KOpj7Wf9K/EU9m77UYdx1rocZrgE/VO9c i2tB8diXuqWnsAdkFvFjf/5Ak0h2yVdJgVMKy8VCp3G5lG7IIQr9VsTgHjiWFjgTEhQd /KjfLw3ULjJaZnh/KLsPlOhjbc3uxh2KJsvQhTubcABXo33CJ5ccb943Q75sjgVWaLk3 Gx2w== X-Gm-Message-State: AOAM532RxvSDXfEzPC7guLvJbXDJVbEN4nTXSxNDtTHWMd0JAWBSU2ey Xfnc0FzFSvEXsbHAlHrjYm5keYMjVVQ= X-Google-Smtp-Source: ABdhPJwug5rc0mqERhpImotorUcCYwoarihXx0+lTRqFD5FEjppQ8ddkDBhGsw40808a0Mef2r9UJA== X-Received: by 2002:a1c:4406:: with SMTP id r6mr2152401wma.150.1631625168530; Tue, 14 Sep 2021 06:12:48 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.12.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:12:48 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 12/21] trace-cmd library: Fix possible memory leak in read_ftrace_printk() Date: Tue, 14 Sep 2021 16:12:23 +0300 Message-Id: <20210914131232.3964615-13-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 9b23063e..60d75d47 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 Tue Sep 14 13:12:24 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: 12493209 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 300A2C433FE for ; Tue, 14 Sep 2021 13:12:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 18EE3610E6 for ; Tue, 14 Sep 2021 13:12:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233025AbhINNOI (ORCPT ); Tue, 14 Sep 2021 09:14:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232989AbhINNOI (ORCPT ); Tue, 14 Sep 2021 09:14:08 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11E8CC061574 for ; Tue, 14 Sep 2021 06:12:51 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id m9so20182667wrb.1 for ; Tue, 14 Sep 2021 06:12:51 -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=glIMpG3HAPgkuiI7fGRy4IPowDQe4caybI6/I/n9b08=; b=le92ZIIS/ayu6LZ37uoWMwGb9riF+EUGKocZAlEKeN4jbg61ObCu1BqMcNovXufh8Y yMortKskDjA3w//W2tV8CVGTmOsOHHnzzQ1zai2OH8RM8dVLD83rpIvmOi2m+n1K9euA i8gNrjgiAXflvwpdY8OB0h8OpASKiBDQ1yK9nq63152fBHfB07fxUqBaTb/ufm0EK9cj xJch+2pxy1JSYps1bcPxYpanXGeZV3DSp/NObQVRZ8zYgOabbkGHGNX9IDN1h9XZXrQv hhsmY6Auxe+tdyOMenV09gD2jOhuGT82PHtMFAxF0zh6rkJApjmar1FT6g+gfdE+zTQf 8Iyg== 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=glIMpG3HAPgkuiI7fGRy4IPowDQe4caybI6/I/n9b08=; b=xP2dHc/kvMbBkGM1nGquPgmb8WemNOsK/5UUbHaeUjn4MANQVNJGTG4ev4An4C6SRf mZUUnMMv8cQJGoOTud3N4qAjOgxn4dy2Y60OtjtcF/yjbOH3ByY2qBo6jDMjnyRtpohM Gml11Sn6zZFze8CFT9N+DUtvglwDTC1zGmFVF4QmAJ18hgVSk2YgOHNf6mPGTEMwOS6u uaaMiy1l0i1GftNyw1+MKcG/5irghClL2DxE7L9TW+4lah+YgXr/U0RzfXpzelP6xVmz 1tGL0r6Hd6YwamhxjsCyVftKiXOhyHeObBFJgLDkAM2PfKaFe8LbWOqX5ObfMxMsMgYg dSoA== X-Gm-Message-State: AOAM532K3Oj5v32m5X0tUWKFvgKcYbNyjLfh4+sgSpNp5M/q0qiK2Ddz IDKlDpNtY9sof6hznyu7MkpcPI8WpCU= X-Google-Smtp-Source: ABdhPJwYneiqdCfI4z9i743Rkk9ELj8QrneaZCNP5Opmzb44JOq5aIQfVSxcnmi31gE2UW7F1xsDAw== X-Received: by 2002:adf:f1c4:: with SMTP id z4mr18984256wro.418.1631625169623; Tue, 14 Sep 2021 06:12:49 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.12.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:12:49 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 13/21] trace-cmd library: Fix possible memory leak in read_and_parse_cmdlines() Date: Tue, 14 Sep 2021 16:12:24 +0300 Message-Id: <20210914131232.3964615-14-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 60d75d47..9fd7e6e8 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -2999,20 +2999,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 Tue Sep 14 13:12:25 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: 12493211 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 7997AC433F5 for ; Tue, 14 Sep 2021 13:12:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5B018610CE for ; Tue, 14 Sep 2021 13:12:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233135AbhINNOK (ORCPT ); Tue, 14 Sep 2021 09:14:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232989AbhINNOJ (ORCPT ); Tue, 14 Sep 2021 09:14:09 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30E0EC061574 for ; Tue, 14 Sep 2021 06:12:52 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id t18so20188104wrb.0 for ; Tue, 14 Sep 2021 06:12:52 -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=/A80fJWoSFzNxEk5fUNHia3MlkikAeaBVf8zLeooVqY=; b=YFntG8st3TJFe9tdRGQP0zbfuNGZBVPiYgo/fAjZmKl4JoJ5Fv2h3Ahxy+fpL+B3Xn C7q+cbDZpOeD4hjeQB0jmpTSyQloxzIX4xomJksWxcSGokavgDdUaii6fYgO+041x9oz yQQR2w2iSqvFbTSA+Y4Cq70eO5ZjB+P3ZXyqyWTTI4U0GPC4ywemwLOKaznZyoQKhuM3 +HEI6EON9BUgsTymNQkvJKXIk8fRBhtpx+ee7O+o2MNHH5TH2bqkba1D6S5IgPByqU9Y Qquc5PbqQU5U+7tJ++pDNHnqXqsCJq5TZ8TbPmBwHqSowwMI1LXymWcfsW3ldtpmNdL4 cMGg== 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=/A80fJWoSFzNxEk5fUNHia3MlkikAeaBVf8zLeooVqY=; b=P7BRRUslnmviGXa3j5MYIwIt5vb6tMl9/yhfI1Qo+w75ibpFInaEdR5U7GnkI8dthV UUfiSzmoINaqDb3COG0KWWvpA5g/2jBt/dOnaN7lnIWRbhgzLXnPBYsLqlJoz0tJoH4H y3jdJU9wVt1SgYm2afoUT8QXueM/UxMssNu3o9bxmKIylXoAtP1bIKnb7o6z7rH4r0OM ZbmIe5D1628QBdYZLlaGYrUISRq/dqaQQRa6kDiNiG9raIIKjPbSwennNTcGqO/rW5W4 cxaYDXjfBVbSF4HJV0rhy/kHNjTf5K+p3OSLGNIFtewKvdXXt7+r6jPaKTh3YGEdhf8K Xc2g== X-Gm-Message-State: AOAM532hucByQNROsei4GPPO1onQFyVc5oVDdTZ0YjR5wGaf1BxsjFxw v7uwnxj7Brb3gg678C3U0pc= X-Google-Smtp-Source: ABdhPJxulh6e11M5RU7JnC+aiIqB4RW7KTO1ktU3Y8X2qjd+Px+pWDbVlQRN8V7goGA+jgVDEXApbg== X-Received: by 2002:a5d:53cd:: with SMTP id a13mr18808342wrw.33.1631625170704; Tue, 14 Sep 2021 06:12:50 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.12.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:12:50 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 14/21] trace-cmd library: Set input handler default values in allocation function Date: Tue, 14 Sep 2021 16:12:25 +0300 Message-Id: <20210914131232.3964615-15-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 9fd7e6e8..cb8b9f14 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 (;;) { @@ -3310,6 +3308,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 Tue Sep 14 13:12:26 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: 12493213 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 748D2C433FE for ; Tue, 14 Sep 2021 13:12:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6033D610CE for ; Tue, 14 Sep 2021 13:12:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233146AbhINNOL (ORCPT ); Tue, 14 Sep 2021 09:14:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232989AbhINNOK (ORCPT ); Tue, 14 Sep 2021 09:14:10 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5DD11C061574 for ; Tue, 14 Sep 2021 06:12:53 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id b21-20020a1c8015000000b003049690d882so2033610wmd.5 for ; Tue, 14 Sep 2021 06:12:53 -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=MyWIX5tRJya1zLsqCa6QH1jp9vXldsg4aBN46KmC+xg=; b=B9puQ+YAXQw31PFzPNclvEXF4whsQfRjIRhaplZgv9YTEB062kKNR1J1mXxWhh/4s6 /xMrZ+CxkO2L2rh1jr5YpHmBU4OEH1+wVJFTwu9hl9fl15AJdD98EFGyr1RtCE3cXYOW 2RDJhzT4Ho2MSPXbc8oatoz9XyDzTG6izpqeQyr+pWipqci74YUk/b8ubvyKQ24uEzUm vF0JCLXUjytX4ZnaJf6s3aDvJ57CVQEYniFFO3uVqOZjMYHkQmO9aehAJtGwjh02PLtC 1DsozRHRAxPYsUmamOqKITfmXPOzvHEnQj3VMUEFU0KLQYJVGQvs942KzeBIGF6GXp5t +04w== 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=MyWIX5tRJya1zLsqCa6QH1jp9vXldsg4aBN46KmC+xg=; b=xNHDjqocZh/0fd2xfP+sxfLjqbchTm2EyQ3O3ztpSqQadstFMIRZJlXHHJ2F6ffEuI beum1R9BQy9+UsOINr6zc2MK/8dREJgZlJy5RxYKbab5gkFPE+msGa5phHqjuvArF1J2 s1xNqq1P25sCYbco4IMq1lXWVUqd+epRL6VrNDvIEFEkj1bxcGmnhxFKOs7Rc0dLQSbi akTJoHtdlctDTsmqG5eRoI54qWXIwQJw0S4jjTGPZoEz5RuB58wFUgrAgGujP65Pe2se 1lF70dhHZ1CIJh6lpqKOHUfn3nsIv06wp3JdOOOvkhzFimT8IM7CXNCcfZ4gMZSramkP AwIA== X-Gm-Message-State: AOAM5321gqjRReCi4MHolWmVfjcTvhAW1TBIJUNSoYw8+ErBU+cigh3i BXv91zBFvTdk9DibJBeQWIK1RNReN74= X-Google-Smtp-Source: ABdhPJxzrBGJs7qnolFMfUaWdO9ZsNpwpDEfy3Hx9UYxAvmlMyvh2xik8AKvEKt7heaNMJDGw1ajww== X-Received: by 2002:a05:600c:3656:: with SMTP id y22mr2183459wmq.58.1631625171886; Tue, 14 Sep 2021 06:12:51 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.12.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:12:51 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 15/21] trace-cmd library: Track maximum CPUs count in input handler Date: Tue, 14 Sep 2021 16:12:26 +0300 Message-Id: <20210914131232.3964615-16-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 cb8b9f14..dc82d3ea 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') @@ -4074,7 +4078,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 Tue Sep 14 13:12:27 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: 12493215 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 AE1CEC433F5 for ; Tue, 14 Sep 2021 13:13:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 98E1A610F9 for ; Tue, 14 Sep 2021 13:13:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233077AbhINNOS (ORCPT ); Tue, 14 Sep 2021 09:14:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232989AbhINNOR (ORCPT ); Tue, 14 Sep 2021 09:14:17 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 71656C061574 for ; Tue, 14 Sep 2021 06:13:00 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id b21-20020a1c8015000000b003049690d882so2033900wmd.5 for ; Tue, 14 Sep 2021 06:13:00 -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=aoAyHoHnzV9YPnQcsKKyNJVwv/Z3UxVNmYzA1f/dnX7+c4nSVV9TCyGPaMcV8lLjso WCGxkM0KM5/ahuqde/4UGm3CmH6eeWu0wzH7Wp76uKWtlIncWd2F0O4sCsG9DnnQAfmC s/2+hMN5gfFAZMeZb6K/9vClNHq0DoB9I1OmhlibzAIzxz8XbbC/r7tm4RG7ZOYgLDIh w1bjMax/z5sohBZrohPnPwa2Qa3ys7NjQZsQRZNCo6nbK3lqpmCkoTon8vO2bNdl4noU qNufCiI3pmP2u8W7YpvM2GV0exaHjZRTM8KbHjo9JbN2DPn7vlN/3IxVEv4a2gQ+DBwv FQBQ== 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=qXCo1V+igM0suHy3IPNyZy+VtJIrPwft79kJjokgIq2kR1S95hHzLpPH9PFyS3HP1D /B1am0Nm9mO4RwlxUPSuxq4WQedpQB7mX5KypBCdbHZ6Q36ZQnucoLLfUd6uZn1/QqgV Gxq0cMAQpMgicM3YKw6fTXSipD1bW8dtr6OHzIt18wmAuTu0KCQBSwa/A2QysplBY5UZ 50FHVbdXpmFqhutJzLsEawtlDeOYK9AgWXNZZgaF+XLDZYoDS1HOrQt1erEKeIhYhoHl EQoREePj+PG4eNlQgZs85NO8Yn8CC/42sF+KpEZcoo3jjkNU6SC+wEq4kFJ7/AJmDomR 3Mcg== X-Gm-Message-State: AOAM531CXfvd576xLOLYUgfPs3BDC9NTdbjAxz5ezAfIyvCZMdhuAtl5 PHMF16Z/uMJzLwxccOzN7rwyb4Sk5io= X-Google-Smtp-Source: ABdhPJzewjnWIxTay/PdQRjdK/yy1ydVcHzYCGLTiobhwd7Ren3ulxCGa4UyuNBAYfi53bBSZMfX0w== X-Received: by 2002:a1c:2c3:: with SMTP id 186mr2144644wmc.43.1631625172905; Tue, 14 Sep 2021 06:12:52 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.12.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:12:52 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 16/21] trace-cmd report: Close input file handlers on exit Date: Tue, 14 Sep 2021 16:12:27 +0300 Message-Id: <20210914131232.3964615-17-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 Tue Sep 14 13:12:28 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: 12493217 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 79137C433EF for ; Tue, 14 Sep 2021 13:13:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 55A29610CE for ; Tue, 14 Sep 2021 13:13:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233093AbhINNOT (ORCPT ); Tue, 14 Sep 2021 09:14:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232989AbhINNOS (ORCPT ); Tue, 14 Sep 2021 09:14:18 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37B84C061574 for ; Tue, 14 Sep 2021 06:13:01 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id q26so20129998wrc.7 for ; Tue, 14 Sep 2021 06:13:01 -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=aR8mUJfokL7FJkQhxMJDlY1F5pSd+3AdZ3mBGqq9CsgOIiIIdO0Pzfr501RXz+x8dc 5Bl+zqGYgOVuIU8OhRKGliQkibP57MpdL3D3RqI2zu8jJhgEvvqz+9MI2ITgdgvvUPj0 ucB4ltz8ihyvUWgW/2vhj93P9Bxpc7NHblTIb3FVYpDv9CJ4/95oWHAlIWkmHsAKA8JT bABxgvEE/mqtG7BLGf9ZcT3VYsPy8tJ71oqNSn+TF0qLS2Tv2vRDNRScC+6w/zkCnZZT syxA9zxH58C8RKuRssVUhlIwwLgpWveW6sJPMEQKIfKhrcU7Jaqjmbg/NkQ1rN8/Z8FX ExGw== 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=g+yZ/EAGD43k0iEDOA9PmCRvVC3cRap/aDOVmXNgQf878aGX1MqL8BTVHbV97hhtLg ew/RdPP+CtGQs8aCkAQbrRvs2VsNNQ7yKrbDpCT8aG7rLtQqjpVBdjw4XJ8enp3DmIHY 3C4EGHdg8MThaS4aiHl8UKjeD2F8G5JsRxNMNbQzo4RMMwcg3Hk5vyfXiiRWydMMcBjR ODN2b6x1/DCmEjDcHvLlLLSAPblNd3SFTRlSjflyhKlFQ2vA7NbvCBFhDsL92HsuIEbM 70L0floWDzUipL4jidegaMGCRJ7BcXRWhK1KohR/yfNV+1scq3SH//ERevXz6RhUd8hD 2B9g== X-Gm-Message-State: AOAM5315PWYULwR5DPQM3G2Rf1q2pbyafaqMuwHJXS2f2DRQ1N3rZsBO oon2S5+Ppjjz16BuXQV3Gv/eTHZylEc= X-Google-Smtp-Source: ABdhPJx+7WbfY/cDAplmLeOLcB2O90lT5jn6xp6fAMGblirsiAbt0RI9xIwf/PdoSNSFIQLHuly0SQ== X-Received: by 2002:adf:d4cb:: with SMTP id w11mr19015214wrk.125.1631625179874; Tue, 14 Sep 2021 06:12:59 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.12.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:12:59 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 17/21] trace-cmd report: Do not print empty buffer name Date: Tue, 14 Sep 2021 16:12:28 +0300 Message-Id: <20210914131232.3964615-18-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 Tue Sep 14 13:12:29 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: 12493219 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 0F173C433FE for ; Tue, 14 Sep 2021 13:13:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 02934610D1 for ; Tue, 14 Sep 2021 13:13:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233172AbhINNOU (ORCPT ); Tue, 14 Sep 2021 09:14:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233150AbhINNOT (ORCPT ); Tue, 14 Sep 2021 09:14:19 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3EE99C061574 for ; Tue, 14 Sep 2021 06:13:02 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id z184-20020a1c7ec1000000b003065f0bc631so2085163wmc.0 for ; Tue, 14 Sep 2021 06:13: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=AA+PGUMl91DR7kV9NuJ9YoaOinBRBGDhoRdn94IqH5o=; b=k58cAhKP2nqnDsecJIPVS4dwIS2xIwJNhFlwfOnI4miszHP0kkXjUqqBZYHrYvwtvB Sy29Et6MZM8cr7q+jvJmlxjYMNQcotYCfYqfX75wUOMT8Q34VefPcgjrA4JwqOOfZ5Pr hS74wM0bd8W5vhHWoys/rrN0MbIZUWF5MY4Y7eN6tnkO6bkh1ij1rOdLq/lvZhKDyvSr ke3+BXpQg3u3xX+1t5ChwFOXnCtRD1e8M4TAIRFv2Gv8a/0pqVioGQmbM1MylpGn+WOZ ADFiIMd5MxY0GTLQtHPgqbsZvLQq01SpvUeL0s5AFuHYL/Y+Qvz/t0b4WP1oBcFRhbQ7 Qpzw== 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=AA+PGUMl91DR7kV9NuJ9YoaOinBRBGDhoRdn94IqH5o=; b=Qctl4kzw0yaNhHqmGuDXxuOOSTqfMKpfUvClnwwJ8bONYh3DOzserONSbA7yr7Ch4n EYK4XPGS+jKPLaj8FLDzmcPBNzmuG0+6ftGvQs5COn4pplF+7MXPFE8IgLZ9CwrVjp/V hwsqre2/lzxDR6ldBqh6TsXc9QmpTQhCuIz8wVIG8gqDdiak8y9I5V8o1b4x7nIL2vJ6 nGU8B9Om8FkUf2Y/CD3J5tPRxPrNV3Ds8ZP3JQ2qaq4TVxu4juQODUkuLHeCd690s7rF BcA2NqwuxeILvFu7HHbPDI5QgAfz3nYyNx8wmjwZqWWS19yFnGu1kEkVwkHDkI+hbco5 ImOw== X-Gm-Message-State: AOAM533KMqqUsXSmzf3gT/eyk8MQuTNbAaaW4HYB9kn6mf6MKC+zBMPx M6ha3O9IJTQPUIXFIkmt3CJPOehoxac= X-Google-Smtp-Source: ABdhPJwMoy74c+wsOX6VsAPGUJtsnxdxvn8bvZiKP2S/AsI3fOZT1qqrZ1DaLNpeeT1rUKNcXMdOkw== X-Received: by 2002:a05:600c:4f13:: with SMTP id l19mr2155657wmq.39.1631625180910; Tue, 14 Sep 2021 06:13:00 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.12.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:13:00 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 18/21] trace-cmd library: Set correct CPU to the record, retrieved with tracecmd_peek_data Date: Tue, 14 Sep 2021 16:12:29 +0300 Message-Id: <20210914131232.3964615-19-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 dc82d3ea..de8e0d72 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 Tue Sep 14 13:12:30 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: 12493221 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 690CEC433F5 for ; Tue, 14 Sep 2021 13:13:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5238C6113B for ; Tue, 14 Sep 2021 13:13:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233087AbhINNOV (ORCPT ); Tue, 14 Sep 2021 09:14:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233174AbhINNOV (ORCPT ); Tue, 14 Sep 2021 09:14:21 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1772C061574 for ; Tue, 14 Sep 2021 06:13:03 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id b6so20126203wrh.10 for ; Tue, 14 Sep 2021 06:13: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=8Zt0veOqDDSXpzCVbb+xlRgowJ7dD1XDzcMmVcCpCxA=; b=iVTY5eGwpgyxkmjhN4aJ5dZznpvDrcMEDY19vN3wGo/Ep1qssr363MhvHTGW+Jc2Jk w5rbA2Q4GL5NjuxqWfH9iekJ5W1tKGBWxn1IP3O2+OrUbUAT1XEXbDOoZudTBfah5kdf 6WR2f6RSF3B5b1Vbc35ISPOKLlr6w+M+2mqWzpElPCzQc4AjuQ72pX6onAkrwgIpxkHC SvN25ANq4C6ckx3HEWlG5ytXqKxshgLJtJTkjIMRmSwQ5cx9ln2SLb5iF6CkfNDcaZ2V /ZicyEGS1nh3l9A9AFdQtoiNm+ndKuJzMAmF4ANO2hWte147pAjgMQ/9TfnBDY5ZuQEB K89A== 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=j3/sT4z95oTKQhGztwOZpJCgUTBPodrpHJS0cQN8+SHzhtzFwg1v1NODGlpYM7Rs7m 7N7qV7IEHWH29BPxIHmRxiL8WDpWBF9sds1kYHKw5r5YBP/74xRfY+yMMSsgu0AGHq1Y NyofVI1JA1zbn0TRVz0yG3NJTKQGGwH7EGr4US9d5schhCuKXSEte2aC3xXt4aAbd8Sy YCtOTxvXslomiS6vQ8CEtrABNMsY+8awa9Je5ZTMZMe9qlLeFFDGGFRNxyVeVinwlrER Z79HDj5CpyO0hOCayzEMnkfCvukUQ6goqrt4HE2tpkJviHdc0NunKImWI6FPuFXMTl/A lOnw== X-Gm-Message-State: AOAM533DofkllWrTp6UctictMmzbaybUVB1O4TnP7ytc9GMNaQysqiFk FCdAukFUdaRNNpAVjP1dwNTi91mCwh8= X-Google-Smtp-Source: ABdhPJxvmSLZXkuih2pP0VaPBayO7cYPUUvwjThWQRB44lPm9Dc3rKclgpsE0/wa8JQ325kye0zX5w== X-Received: by 2002:adf:ec0d:: with SMTP id x13mr1330789wrn.38.1631625182031; Tue, 14 Sep 2021 06:13:02 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.13.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:13:01 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 19/21] trace-cmd library: Refactor APIs for creating output handler Date: Tue, 14 Sep 2021 16:12:30 +0300 Message-Id: <20210914131232.3964615-20-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 Tue Sep 14 13:12:31 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: 12493225 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 49312C433FE for ; Tue, 14 Sep 2021 13:13:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 359AA610D1 for ; Tue, 14 Sep 2021 13:13:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233206AbhINNOX (ORCPT ); Tue, 14 Sep 2021 09:14:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48442 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233174AbhINNOW (ORCPT ); Tue, 14 Sep 2021 09:14:22 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A985AC061574 for ; Tue, 14 Sep 2021 06:13:04 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id d6so20127527wrc.11 for ; Tue, 14 Sep 2021 06:13: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=4u/WomWInuq/AAtXYV8ijeEvhbfS2Yb4+CdYwOhiQUs=; b=k0+udlQEeSjk4e9GZvsksZ8OMwJxY39zxmL/NIBKI6nzwub9AjihtZndmyII3LJ3ru g8OiooVDYLyXE0ylNgaj2YbD6m9pjAbdNKhrt36bJOgYq3Ebv94daesf/W6AvbmlqfTs +7cZpL5ZviPnXBwasxfFhqvbCkRFY64FxApQJOzRogW7agDDfGL0mMTAq07Wf8CemdDi FYHXi5LhzWptKBIGpg5wX70lbuok3UyBMqHHlsUetQXShL4bkQ8M3+pBMfZPMyM9AEJL X9uN1krOAcsYN/pzCp8QoHUrbv/3eZuxIlS8v5EE0A+bch9XYcrqZJlCRSxfcUtuJ8Dj LX8g== 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=4u/WomWInuq/AAtXYV8ijeEvhbfS2Yb4+CdYwOhiQUs=; b=ISNZut0YnK0S+nCHz4gv9fpbEjQhdIcSeHoVTeTZIYWXi5HqFOdz1+8mJdCE5GEOvC CMJBnzN+INDeceb+9qQcGeaJYhJEgrTkAEwATQ0XVjWluu2N83QdiTnVptFiKxIAAV2X wM+P+7N8z0NYRgQY5LchvCSJvjhMs0i8pvaFpbZJI57W5hyvnpiQR13VeFSjnbUWWt0/ 7HxSW54SLFvzfahveF0YsmkeWss+5xOYn0SjSfrQXakivBF0PTMDZzW8VeclzMxRHCq+ nA3MMIrpycd13VlRBGRwypsZ/MERv8VzKGzvX2IBHL/i9moR+a+uZTdZGWmNO30hI6cY sPng== X-Gm-Message-State: AOAM5308Gkq0Q4MEAtgJCM9hc4pkH2J4wkGVNQbwcLQgqTTFQSheLUx3 YtamJIcPqV6/RwquKv1wHWlNmViC9cw= X-Google-Smtp-Source: ABdhPJxgP7/DO4f1V0vTrPcaa9kccjkqVg4vpm+lbyziXVJHGitd9wBbtwqtSmkPkRNEBROrNDH4cQ== X-Received: by 2002:a5d:4f91:: with SMTP id d17mr18975902wru.285.1631625183092; Tue, 14 Sep 2021 06:13:03 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.13.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:13:02 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 20/21] trace-cmd library: Refactor the logic for writing trace data in the file Date: Tue, 14 Sep 2021 16:12:31 +0300 Message-Id: <20210914131232.3964615-21-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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 | 307 ++++++++++++------ tracecmd/trace-listen.c | 2 +- tracecmd/trace-record.c | 18 +- 5 files changed, 235 insertions(+), 119 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..c1bfcb66 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,52 @@ 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; + + 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 +1509,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 +1559,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 +1581,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) + data_files = calloc(cpus, sizeof(struct data_file_write)); + if (!data_files) 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) - 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]); - goto out_free; + i, (unsigned long long) data_files[i].data_offset); + if (data[i].size) { + if (lseek64(data[i].fd, data[i].offset, SEEK_SET) == (off64_t)-1) + goto out_free; + 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 +1716,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 +1773,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 Tue Sep 14 13:12:32 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: 12493223 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 6C587C4332F for ; Tue, 14 Sep 2021 13:13:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 56007610CE for ; Tue, 14 Sep 2021 13:13:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233113AbhINNOY (ORCPT ); Tue, 14 Sep 2021 09:14:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233199AbhINNOW (ORCPT ); Tue, 14 Sep 2021 09:14:22 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95CF1C061760 for ; Tue, 14 Sep 2021 06:13:05 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id e26so9243951wmk.2 for ; Tue, 14 Sep 2021 06:13: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=rgKSGpz75JPloOaC/1DcOk+gg+XSb9yeocHeU0wjVV0=; b=cTvlKt9f01Pb2dzxB7s0hJ7ArSj2AjPscGpP+TFjag34soAoUNbsuIusL0i5InLenE g6LpYji2PU1RcRel7se7urdLV8IlmmDO2UqQaAX5alNxRpUsahPyByg2locNXPLsQpc/ k34MuKTKbXd4AOGMmUr7ULvhXP6amYKl24O6TkBI2bFUmDN82RPz6Mz6Tp/nz19wYKLl gG3BABuLii6VuJRX/GGxxiW/nqM0nb/v4kb4uNGyeoPGLTWt71PPUE/veGJ6sUlY5xBP 6pl6pAIpVecJ4t4/6GrCAXFvuCi0/d4XTv6bbnZfL/FQEBME1jjM3TUGHriVapu/6f5c XG4w== 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=zBHv919F16VpyUq3P6nKjTkQRrP7POakVL+MLW+Htd7XR4lncOxas+dpc3x6No5ufI V9ks5GEpu9neGY9m2vYreUHahrDOAWT8LY2Ze1l8Zq5Ziw0X1yCae7wh3Tw6omBZmp6Z uzEXqJn86MlV25W/MWlYTs6IhytRnr8m9snmK1MISu5glqi6ueSZ1RgA+Qf+a+4otp/D cUr7byaH9JP2MFDiT5PQPMSkzMDnlwussPU0ND8cJz5YiVITu6avZsOMCpXwxxyrC0YU ngyzHbCWV9LqVDE7sxttQ6Ztx3z7WfRh9NIFQD/TEwNp7Jk9wp7MET8PgwB7t7LXWSiF zMHQ== X-Gm-Message-State: AOAM532FKRMYl0M4kllaMsfdaiX0Qu8bldNfj6I4DUgydkTMIygC0p+t QcyZ7rEavhaXJUQLE/e4+lc= X-Google-Smtp-Source: ABdhPJyLx8+gwlE8yt89sd70EIE8eQbmRIoYzHxTVw0lawRIyawktEqqdWIZyhis4fbfVzBsB4jFJw== X-Received: by 2002:a1c:4d01:: with SMTP id o1mr2140837wmh.61.1631625184200; Tue, 14 Sep 2021 06:13:04 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id k17sm1910221wmj.0.2021.09.14.06.13.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 06:13:03 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 21/21] trace-cmd report: Init the top trace instance earlier Date: Tue, 14 Sep 2021 16:12:32 +0300 Message-Id: <20210914131232.3964615-22-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210914131232.3964615-1-tz.stoyanov@gmail.com> References: <20210914131232.3964615-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)