From patchwork Mon Sep 13 12:27:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488689 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 79786C433EF for ; Mon, 13 Sep 2021 12:28:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5C4B560FF2 for ; Mon, 13 Sep 2021 12:28:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237447AbhIMM3U (ORCPT ); Mon, 13 Sep 2021 08:29:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235143AbhIMM3T (ORCPT ); Mon, 13 Sep 2021 08:29:19 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2204CC061574 for ; Mon, 13 Sep 2021 05:28:04 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id n7-20020a05600c3b8700b002f8ca941d89so6470702wms.2 for ; Mon, 13 Sep 2021 05:28: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=q/XpTU2XhoHbHAqoA8x/vtqptp62mgjiQRNtFM2Rvqo=; b=DV1ATpx2wambAt9zJ0wM3pQX3J3L7ytUsK1//X8AnL+6xMKZ121En58ew9SPqbV0UM CMycTAjOisCNzll7N/J8QRPPcxrXQEiZ6ctTEMVL4qibIH8HHEuIa1nLinIoxJVx1nIT CbGZVHtTHI9pcfGlKCvc9ETvPQ3jgUsHNL3LvqCx4B0zcuXY5lJTp2X9GGL+M45iMjr+ FNB8ySFl5bjlJ6y9APW4iu/TxgjDCg4tGpAZBs9pXgIEv5FxatM+sSjm4YAu2QYxT97H u7h7xHMIh8putbpSMVjOyYAtpDV4KMqJqmN1zlOM9ItvBjJT6e0Jwa4jVri8IAjUrAXR P+Nw== 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=wq3fY81iBTIwKy9btAWY7jrDEyZ8BLKp8BP3pqpkTGe6O0Or65/5ZZVtzV+ruW+5Hl tL69FFvIWxn+ExM/LGIeO2CLskAc3e4TllrheAOCyXPYOWe1Sx31ZM9wXb/qyWR/O5oy LXgFptqVF98SLMe83rdpt2DlxNY5/kFXmfdiTKLhSfUSy74ldb3PaWkTH4/36CEzXkh2 DEXbEXx+quFknQD6VxOEBk/lISmdJC+xiFbmb/ed00MjuFm4SBSNZP5T1apY0QHfnReO yxes9McwyxPSKWm0vBsQm470P99kq/jGuEoJDSdkKFmGqq+Huzm7OWNcKXfacAEofyKD AdIA== X-Gm-Message-State: AOAM5306dhYGWsYcy/M/6IztgR8WJ0SVRugGfaolcESO35+sW7A7Zbi1 14HdnLCwtGFjQa4B7P9jsvE= X-Google-Smtp-Source: ABdhPJx3f/FMDGIkRSSCKssho8/91m8Do/pHuvLjoIxe/An5GLKZjPVigxfCNRrpmDmloTAN/1P4hg== X-Received: by 2002:a1c:80c7:: with SMTP id b190mr10769723wmd.87.1631536082749; Mon, 13 Sep 2021 05:28:02 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:02 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 01/21] trace-cmd library: Read option id with correct endian Date: Mon, 13 Sep 2021 15:27:39 +0300 Message-Id: <20210913122759.3672494-2-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488691 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 ADEECC4332F for ; Mon, 13 Sep 2021 12:28:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9122060FDA for ; Mon, 13 Sep 2021 12:28:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239946AbhIMM3V (ORCPT ); Mon, 13 Sep 2021 08:29:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235143AbhIMM3V (ORCPT ); Mon, 13 Sep 2021 08:29:21 -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 2F0ABC061574 for ; Mon, 13 Sep 2021 05:28:05 -0700 (PDT) Received: by mail-wm1-x336.google.com with SMTP id d207-20020a1c1dd8000000b00307e2d1ec1aso691311wmd.5 for ; Mon, 13 Sep 2021 05:28: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=rHEDizwZjviRdumQYx/LHW1ieeaoKb8L1up3BfvtlPc=; b=ULrrbclgz9oorj2mMmgYlEAF+g6T2b6eT7r26V8hlcuoBLDIR/O/hrLgkjFVwm8tyu sMAWNDclfcYTcAYOXM4RueAq5huXHfqoiX1fIOszDWXdDcwo3YwW6dme3qreCd0Xad0F AtwkrOZI0AS3kS7tJyFrUJqVb91oa75RjiHLXo/Sfk9wT/fnn7+g/DYvjQjKchDWo21y mkGcD4xbSJheditpGMUXQ5DETLuS/s+Q7ZPjk4DocogpuT5hVHN1ENisFjQu6lmmMqMu 4toAU5HbiUBxJrliB2tpVCfSDuk9++Yrdw6I3cbtzVymkb8YihnMDOGM3lQva4upv+uD PZiA== 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=0OaxxLscvF69ZCImo2ZdEmeK5XafWK+F1WmGqOi5LZT7eXK4gY+xdAs0sUi/wOhA5S VGe6eG2sYVFC3sqcBZEFxsm988Z9yHvDwBO4KD0pbjgVAU+dNJ4WGJ6d1W5ZDp0hRQEq HY23Vm3DtEjh5s0H/HQGvGhWoMMdBrWKjVskglzZexD+2+H141ia2dXMP1nf9JOG+oas 3jIEqfBbsbeT47ACcu5f8Pp1xNkQASj7gqZCjnanqGqC0KoZTrq2iskc4DF4di62PRpn l3cNXsuZzp6U1y1IPJ2CwkvntWY0BSIeGTUbYGwq3iGLK/vMobtxjatkY2ZGK/coXb1g iJLw== X-Gm-Message-State: AOAM531b2+u9YMEvTiGZXw6DLLOk+vQbu0+mTfcZrDdYFT19bBKPQoXl Bg7k2yDWj39rmvi7fE9PZIeOBSuFs9c= X-Google-Smtp-Source: ABdhPJzkpS1u7dyK0GMTHQWuFHn8DRE5866SFpxF1G32ViG5DwjbZP2/4gvsXdB9RaX4doPaw8H7kw== X-Received: by 2002:a05:600c:ac9:: with SMTP id c9mr10742955wmr.14.1631536083784; Mon, 13 Sep 2021 05:28:03 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:03 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 02/21] trace-cmd report: Fix typos in error messages Date: Mon, 13 Sep 2021 15:27:40 +0300 Message-Id: <20210913122759.3672494-3-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488693 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 68F30C433F5 for ; Mon, 13 Sep 2021 12:28:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4C57060F6D for ; Mon, 13 Sep 2021 12:28:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239963AbhIMM3W (ORCPT ); Mon, 13 Sep 2021 08:29:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235143AbhIMM3V (ORCPT ); Mon, 13 Sep 2021 08:29:21 -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 255CDC061574 for ; Mon, 13 Sep 2021 05:28:06 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id b6so14387237wrh.10 for ; Mon, 13 Sep 2021 05:28:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=t6f35bOqeR513Bi7TdiB4HXAWp6UmqxXje43WtBoeKo=; b=HQdR91nKg/cQGO+kCRG9gOXIpsM3UrdutBuPBrY4/HxfhwwwFoj9PJE6PboLt1lVML fZyjxv4MGGGCJ1wxMUbkzb3Im7+mBvc+DZ2zs48t5QmDLw5HdO7G4idSfTuaR4K3vKRz YZ3J07hMHcdyxnhy4LExv/HHYCYC/SdiRoHzDzJQxxi9M4tBDY2G1XTDOBSC47WnndLa Yqe8QCmyJFu3JbElmKdeBeP5Ua1frqjDzK5P/nGKjl7rE02VWIdv+52z6OV7mbB69NAL Ra1JiAaxyzutQeQUdi/LF+zE45xG0hcDK4O/wOAm5FutvD4kDqzMu3OMTssMyjD9scup nTuQ== 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=Bt5eLGNnn9msRpeBD0jYOJNKEA17kn+50eUdz1bcWpuYjYQDy8oB9L8IPbO2/aKgG9 d4cQP2+KXv95Wt3KL8BxNg491Lg7Ywiuwzv/qWTYYOcHh6cmXJdd/nL70lgjmmtzkiOg ze52Gm9Ud7ngzvaOo3FJmdYwtuXLNVj7YuUw3X5hAQjY9HtZx1Ac1v6TEjYqCeU9bxoD pZIw0JOBUm92u72WhjOZxhX9YDooZ+QJKhIA8d12xakP5O+DKGr6sl9NuvTKsF8pWrea 2ilbk1F2X8WCvnfbTjekqlttSNII9J2S28HBO00sZFQqZd3EYBtV3kgMqbTO4wC5ScoC 41qA== X-Gm-Message-State: AOAM530aKoBOcu8tCNa7KKSuoWa4sPpaoKz5B2S3cGK9kp5FqDgAVyz5 BPXQA0SWVzv5H2VwHbCljITLJkXn2e8= X-Google-Smtp-Source: ABdhPJxaRoHMprEexklbs1A9qMMGtORt3dk/x17HUXwI3Sna4v5xw5rJwdt6MGVfV3OCGuPV0O8a2A== X-Received: by 2002:a05:6000:124b:: with SMTP id j11mr12483136wrx.147.1631536084820; Mon, 13 Sep 2021 05:28:04 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:04 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 03/21] trace-cmd library: Fix version string memory leak Date: Mon, 13 Sep 2021 15:27:41 +0300 Message-Id: <20210913122759.3672494-4-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488695 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 4ADC9C433EF for ; Mon, 13 Sep 2021 12:28:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3A1D860555 for ; Mon, 13 Sep 2021 12:28:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239986AbhIMM3X (ORCPT ); Mon, 13 Sep 2021 08:29:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235143AbhIMM3W (ORCPT ); Mon, 13 Sep 2021 08:29:22 -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 13413C061574 for ; Mon, 13 Sep 2021 05:28:07 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id g16so14443419wrb.3 for ; Mon, 13 Sep 2021 05:28:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6uflLdcxtwKc9UdYaXGxzpx3D+nzjM4vg/8RpU5kByg=; b=MTHmD4C2FchSecpCLQ4LJQiM+ZXNxh69iycoy9eEMfGAAP8b4MRYZuyAauc1YJNAtn g0T5LUj2ycwAIxg1LF5d5vV5LF11mxZtfQkarqSR2SPz81YEP76/CPQwcBfVGjxSCRD3 V3DHQFT1FFzIkWwZc6gdhDk7ZT+g3WzK2sLOd+uvCKpPnI2pFX5t7KinTpswRfupua+T JuKIWs4edTr0TY2WdfxnSeMULhh303+UP5FuoOZM20pLV7CsnhcK1nK80UZpi1GqLyqM ar9jJrCZZFWTzIbOZdYTzy1Um9p0V1JLqhkSbSM0R6Ftt5NIm+HH94ltjfnm8JNJT4F8 RMNA== 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=BAbm8m+W9AvEJWeh3bH6pErHuzWowRDxsFItFLCZ74tzG29RDfE3Ln/H/E6BWJOWjN PRQ/mA14IEH8dDHvdFzF444nqqEXQb1D8wCf4RU+hOfOKyl1AMlLOWBAmdochfosmi3Z aOQBglwI92sf3RIAtoiTonP4nRMe8Oolb+KdUNiX+nyurlV3hg7TB8feFpudA9eURzfh LvJk8eHvI3PHpk/Mr2du+DMaQDfZUu+cApKMd/xrNSGTbMqAvTb2tUiVyih53S7dG4v9 yJMreWLart0DD3//STgTIVTdnHV96eIFx5UYUefgurCSKbFiiBAA37/kJaHSWaZ+2om5 XP5w== X-Gm-Message-State: AOAM532XmfHYuDu05xg0FwWOE9NOGAvDGtGR7Q8A7r0RqyKg51urBZSH 0X0J8mGMokqZtxJXghM80DCBS5t7q8g= X-Google-Smtp-Source: ABdhPJxtiOUFqup0iR2DWj7Z1/VtTZ+bTArsINyeYHgHOMO+2egFzPHz3OKy8taQyD9arUWCrAXgOA== X-Received: by 2002:adf:e10c:: with SMTP id t12mr12214685wrz.36.1631536085729; Mon, 13 Sep 2021 05:28:05 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:05 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 04/21] trace-cmd library: Fixed a memory leak on input handler close Date: Mon, 13 Sep 2021 15:27:42 +0300 Message-Id: <20210913122759.3672494-5-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488697 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 46F74C433F5 for ; Mon, 13 Sep 2021 12:28:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2E6DC60555 for ; Mon, 13 Sep 2021 12:28:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239992AbhIMM3Y (ORCPT ); Mon, 13 Sep 2021 08:29:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235143AbhIMM3X (ORCPT ); Mon, 13 Sep 2021 08:29:23 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11526C061574 for ; Mon, 13 Sep 2021 05:28:08 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id u16so14415454wrn.5 for ; Mon, 13 Sep 2021 05:28:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZeyT15SqFOCewNiJ5wWZIoGsE6RejnJ66ku3xcCgS5c=; b=Edi6MP/+qAiUu0CfD5hyWUzQlRZf0w677ECBuXP4lBDrf/8ZN2DhaeOFZbk9SrDz0n VqUoSecC4pBLYs6Egmy/6rzgtfaj92Za8QmYujZKRQRla7+TtgbvmK6Ptni5IuBBCeWj WTWng2/eERViB89wakwKxzLTLO/2WUBTpnMTca+jPNLfuEUzaaThaGWgq0JCaYXJmn3t Xvy+NGCza6gf3hVLqZuDMB4O3XrKLunkb2iLD3tf9kz0/sctdp1gBDie78z2cliwVxI3 H/JVtAg5XKwp1xSHhoUTEfbBfoThRKNo5vTyPAF6q1v73IFjlv+RsIMNShPkDfknHF3U SP3A== 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=AYoEDd9gKo6ojFjBQyVTyHEu6oqh5Iy5mjM1SPHj9fJ98dgLfc4QwYGcCyNmrettzl KCFYmTn7cNjGO39PrzxPsi6Eo4q0QqPdP3mgpqUXHdqdJJK5k6/qs+ZaJUJoabtmeInl SRrMXaZN3royIyb0lgKqG90rUYdJfhFSoaBcfLAk9Eyx2w7uEHW7Ige+OmU8b4YieeXt vA76XxttE4GPTxN35V/hhDDOc7Xcyea3BDpTaT6f7xUQdRe1332o3DYd39MS/4DmPPp+ GL9mjGk0LjexeXBA62WOXkFSEJjkCFn2outwmHEbGql7EsbRwVo/2Ld0/48zXZidWZJV sGAw== X-Gm-Message-State: AOAM533T+gvAK4o3AlZQuJRi+Sd1S4NmLDqAhIywsFiDop0iRLNHm8kK pItxB9imIyLIzhe47WWw9Atm6NxXuSA= X-Google-Smtp-Source: ABdhPJxyjL0wRPPkSax9wMwdXfAubggfzUneLWwYnXJccekhKDMH+SKlstcewJm6EuuNhy1+IK/SyQ== X-Received: by 2002:adf:cc82:: with SMTP id p2mr12561553wrj.5.1631536086740; Mon, 13 Sep 2021 05:28:06 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:06 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 05/21] trace-cmd library: Do not pass guests list to a buffer instance Date: Mon, 13 Sep 2021 15:27:43 +0300 Message-Id: <20210913122759.3672494-6-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488699 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 80A4EC433FE for ; Mon, 13 Sep 2021 12:28:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6BA6760F6D for ; Mon, 13 Sep 2021 12:28:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239993AbhIMM3Z (ORCPT ); Mon, 13 Sep 2021 08:29:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46564 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235143AbhIMM3Y (ORCPT ); Mon, 13 Sep 2021 08:29:24 -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 24364C061574 for ; Mon, 13 Sep 2021 05:28:09 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id m9so14435783wrb.1 for ; Mon, 13 Sep 2021 05:28:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4eNSf3fXIhuexSWrZzu9VRBujGIACDgGvnuES2rhjo4=; b=hiJewWSQQXKW0uIlYcqh6RmLjU+dhMr1QjgUIJuIeP1Ya9O+1r0sM0MY9QLP2nNEbn paYLqmj9RK2DELUzohg8dcoSJWjByuifzGMVXwW+GoqZSLDr3RosokXIfin32RZ8DLAK p2QgLxMcW/DqWYJLp3P3sbeMs/ZSOXXm7lj89WnQGJ8Z+HK3vTEuN1T2SgSqh41cd8aN sOEWweFdQZvLsYtEg4viuCk40JOSgwGyA5W3Z0yY+shCnYqYxTVzs+1ylNkM3yniWnI0 ll0Fu5pXoKsDXVGm9prD8QPs6bl2io0ytuxMfFn3oJNlk87Q0jb1Bi1NWMWmt5KzxMo0 0qwQ== 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=D804CpcfkWiBptBtLlenRoz8brRkV2yx70tvkTAazr5TLJ+LE2pczwS85GBo91RPhk ewfupvkfdc2bp7Xdpj93N9aOvdTjoL7nc32M9VdkR0sDcwzMTEemZSFJtDSgYbNe/iLR +yp8PBU3JmU7gj+DcQ0n7l47QEiPs8iir+ti+4qIy4tFlZGvVbvQCwMr/LtpUtazBn6t uSJH7ODvHvx/jcT9jnLZEN51LJrpmwQAsavh7eVccfY+81vAlMySfwP8tcInfGpccgDU MpkRbYs+iqzgy93LnSpjKF+cR2bYE5AP80Hs3gQxCJ59DsjXw7ZCPEU6YlqLt5ssZdBE eK4w== X-Gm-Message-State: AOAM531wGBz15kgch1D3egU66d76VUjPxKFOW4tg6KswtsncT9pD+k0T MTKhHNvHW896ADfIqJZNTDMBcD078OQ= X-Google-Smtp-Source: ABdhPJyO990rGYTxB4ZVqbSIG9EihtRmdzrXmyfi9EoN1PeNuNHKApVeuXYBnVQ8zJhZ+0nUjCZ4wQ== X-Received: by 2002:a5d:58c9:: with SMTP id o9mr12466066wrf.279.1631536087809; Mon, 13 Sep 2021 05:28:07 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:07 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 06/21] trace-cmd library: Set long size to the input tep handler when it is read from the file Date: Mon, 13 Sep 2021 15:27:44 +0300 Message-Id: <20210913122759.3672494-7-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488701 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 D2AD6C433F5 for ; Mon, 13 Sep 2021 12:28:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B53D260F0F for ; Mon, 13 Sep 2021 12:28:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239995AbhIMM30 (ORCPT ); Mon, 13 Sep 2021 08:29:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235143AbhIMM3Z (ORCPT ); Mon, 13 Sep 2021 08:29:25 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C9DCC061574 for ; Mon, 13 Sep 2021 05:28:10 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id g16so14443651wrb.3 for ; Mon, 13 Sep 2021 05:28:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=iHbl/58swxEP+2R4EUL7BmGjrykf7sAqFNSgdDuIakQ=; b=HZWRQJLq3FWXR2VmYiBdGWZlU6AuQu3IZ0T6ED+UdoGjzQy5IEMIJGSg3EYypvo97E R7glxMJt99pUlAlEgq/joChrQd5dAs5mQzgNff0qfIZOgHRScGcBkZjdZJEFfRrMwQxJ Rai6bI0+P9Aikelb/h+FQbsCHYY6/mnWJYF8MASB8sELiFqahq5/gNnPAPj8SMaxdZ8B Vdl0XgeYEKCkSAbpzUM1wOsH5qFrKzrSUgPL0GQHUIawB4OPHU9qLslU2ZNAANI6dlC5 U4xJt3ASxcGaMecv+peXHj1cG+q1UyXVEqVayd6gvR8lWR7AoSV3gbWWXsPV4aZq9mG2 hqRA== 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=3dcsaVzytxgW51vd2QSEM5O2Fahc59bo9vK7rY6fRc1pLoXA/vcUkpQYcMxuXiGY4b JRiPVQ2hnWGzJJvzYU/es34DtPcY1JbSMFmB+cNhwEmXXSk3oC+Tn+mtTzAd0UITMNDJ 2DUf6iroAIQDmQuQw46KD26sSI+6sE+ZpWEyZnLIXmw2Zh4s6vQyeyItaIR4E0HesPOB ypyxsONc8UklXoIltNFHVM0YXAPOmGHwRkZih1HC7evlklFl0M6w2rvev/+5Cq2AX8lY bu/0/HyzJLQtIwed+M/NtR/NXhVP20yVDwax/DUsL/HGhkYm9Fw7xJqEPuFBn4IM3yq7 mrbw== X-Gm-Message-State: AOAM531ArmvOkDuXxY7Dv9R3Z9n6193LTPGjl3zxWhCOK6kAykjMETrD Z6xrCeXBjhXKfguskWR7tFONfTiF1x4= X-Google-Smtp-Source: ABdhPJydfrCK30jWCtfkF4U94O1UIribC1s8hodUPNlSY02731DRr20vQ9+pNHTR6vmLVe84m2gpRQ== X-Received: by 2002:a5d:650b:: with SMTP id x11mr12135512wru.350.1631536088758; Mon, 13 Sep 2021 05:28:08 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:08 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 07/21] trace-cmd library: Do not use local variables when reading CPU stat option Date: Mon, 13 Sep 2021 15:27:45 +0300 Message-Id: <20210913122759.3672494-8-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488703 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 6F868C433EF for ; Mon, 13 Sep 2021 12:28:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5117B60555 for ; Mon, 13 Sep 2021 12:28:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235143AbhIMM31 (ORCPT ); Mon, 13 Sep 2021 08:29:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239998AbhIMM30 (ORCPT ); Mon, 13 Sep 2021 08:29:26 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12289C061574 for ; Mon, 13 Sep 2021 05:28:11 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id b21-20020a1c8015000000b003049690d882so3991792wmd.5 for ; Mon, 13 Sep 2021 05:28:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/iP4a8bfrh1K3oUHJHD9zBIH/f3RsH2LCFQaSP0YOGI=; b=X/kAJ16VgYbUbKXLJbVV5YgT/4lnLF5viPOLZFdfPk6Z1pMBHD6KYnbj0NN3vwmi0d fNj2QdttYqfq3AZHFo83i4FJoyeD+73eKuNc+L6qqZ2nyzSugeRlCpQKdw79tc+nE4/6 c2Q0oPuVF7QiLP0R6GwARb0LNTo8vdc8R+Ibm5Oj49xAWAzzMspYivT4dPn7BW/p7WAC ttMKb/it0YEoXDWp/4xx4a+GruRVSfxLe2HTJcGky8erdyN00UvndPMRw5X9Vd+Nb/Oh 0etUWgFKdrJuhr5TJn9zkQcF+KNtFZaFLM3+P1/OftQEbyfHgQkbrhfK4K/8erXVY68w iTQQ== 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=jrgcYNKqzoP+eEfvoly+wwAsoIB+7aJ/J/3EGLqTnDNeWBlVAiPfmWskaIkpe8Vwfo AL5hwwsefW1LIJ8UVtPezL+ffA2bDvK3TXrRdgJSpKNSMtiwIXNf1iPr3z+rDdjgbdif N1ATw/z/7DB2YsGu81RtppfUpuXvZFGC0GEFgVw5qne+k2kRioPWLRLLoOfbjhLnfUrv mBlGvPpRw3WOnnwiuI0kA6Gdw+0TzcPwye0DwDPd5VwRCtEoYmjZ8QoUwqfzBX5GOgwP 6uYhHYHAtd8C/Lmms2GEAiyvD7SzY/dRl6nfxQ7BILZPruscqyVC+UWR2x6fhl05nWrw MJkg== X-Gm-Message-State: AOAM531r97UFR3nnUrLBD9MRpN4NFMS46i0frgPmVDs9xy0jd9TWDn1x ZA+Kqn2Za6glXlwKtns++7SdV4gOE5A= X-Google-Smtp-Source: ABdhPJydp8Elc1hC7R5iWLe1yYJdlLyShR0OUCHdLMSzuIxJqR3SBQoUD/4r+QXjn6KaZ0cyP9CeRg== X-Received: by 2002:a1c:1b10:: with SMTP id b16mr10701950wmb.194.1631536089691; Mon, 13 Sep 2021 05:28:09 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:09 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 08/21] trace-cmd library: Reuse within the library the function that checks file state. Date: Mon, 13 Sep 2021 15:27:46 +0300 Message-Id: <20210913122759.3672494-9-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488705 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 14693C4332F for ; Mon, 13 Sep 2021 12:28:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F041460F0F for ; Mon, 13 Sep 2021 12:28:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240000AbhIMM32 (ORCPT ); Mon, 13 Sep 2021 08:29:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46578 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239998AbhIMM31 (ORCPT ); Mon, 13 Sep 2021 08:29:27 -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 DA30FC061574 for ; Mon, 13 Sep 2021 05:28:11 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id t18so14459372wrb.0 for ; Mon, 13 Sep 2021 05:28:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=3OTYZuCAo5R2kjph4kWPglVUSjijOi7+4l6GtPJgV2w=; b=PAL8AM5oR9xU5++PkxNRjn2cI+tIO+E3oYz+OnlLKvjTksJvmel+R1TbYucLLBsgjy xvMDbbNrzvXW2bxJ0VutFLFO03vyiyk0ZgxRPCmavQNJL1Wwc3UOsevLzI0KanD9Cmyx Lw/UFBTFufUOt0MgPTRNr7YLcJAZpoa0HXv2mDMKPTKR8CDWhPa0QVTdYLTW3vkf9YWK lMPT8EB13k9Z4Y7lkyDIS4IYBFUmKrUIP/PZFRqa4GfXPYg2mG23zQQtgTvjKUx6nOv7 iz+jIVq/phcgAnEIDz47snXMBNskIEreRPKTO8n9B0O4VRJ69llS+3X65LcM2ShOOtBv xckg== 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=71+wxbS1fm0aoodw3arY0r8Mm+hm4Hw1vTvPNZqey/jeUfCYaThoOvDwzpa5802Awp rk88VhMoflSiZ2objI2YXdzfYNrJVDvjXmoPh4fmqI2TEmGUhiwJsiCA3jIznkhzKyze RU1c1DVXaOA9XkHgrGsfyUO27x/tDjwYwdwUhjyaCUH+YHuSoRZBhxBANI+5xoOQ+i4V 4WMMYxO9JA5uibw/v8NcqBuAsXKSkNBuCpDcM97osXBxc+676Bz2UZTHY164XwTs2StI vPHYR3wrQspPEkb37F+JkGSKHphD+m60PR9mM6qFymeM/4JEOiUuCKcwLHUbRjkxECez ukjg== X-Gm-Message-State: AOAM5339LI4fFBHrmlZ/x5+Wq/WrjpNOa5TCAXzRgGmWMmnoDcc4fVFL PbVKmW55tCy/L52imI710kkYSzLNkmI= X-Google-Smtp-Source: ABdhPJw6FYPzOy0m1vbg618ShVxKvvwFZo6AYif+wGZr+N2jT7mY//uyPpTPeL56N/P11LkUj5HQNg== X-Received: by 2002:adf:e406:: with SMTP id g6mr12323940wrm.172.1631536090545; Mon, 13 Sep 2021 05:28:10 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:10 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 09/21] trace-cmd library: Fix possible memory leak in read_ftrace_files() Date: Mon, 13 Sep 2021 15:27:47 +0300 Message-Id: <20210913122759.3672494-10-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488707 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 413C4C433F5 for ; Mon, 13 Sep 2021 12:28:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2D22E60F6C for ; Mon, 13 Sep 2021 12:28:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239987AbhIMM33 (ORCPT ); Mon, 13 Sep 2021 08:29:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240001AbhIMM32 (ORCPT ); Mon, 13 Sep 2021 08:29:28 -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 E57EAC061574 for ; Mon, 13 Sep 2021 05:28:12 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id 140so3098968wma.0 for ; Mon, 13 Sep 2021 05:28:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0j1dgOlKG6MfWbaTr4bXRbYYtvn+fCKUl29MSaEewTs=; b=qQoJeDPJPj07xrnYfzYVWHxfDxzE1tEKZn1mUqu1jyu/5uDAXrZ+P57KvnsVJ76pFf PUDcVJOhxZJAluObM5apMEwHKRQUnJXjaai1vvFECIWWyFWFKwVFaFBxTUvcxWQWRjNe wedlDyXekrPu1hrOcNKoIdr3VR2HVMNyrBpNYLvOrSjpluiz5L4J6tOA6athXde4ZpdT CtVXmePxYF7NvtKkmW8rqv0EUDrH437SM5dGEKdoopvr9TZP4+hlEPkqzNNXSXIOyGLt qyWAU22iiOA7D4nvnu/XsaKJtu8NbPUx1st1+Dr3gMTzkSTxTdVR6BEsdnaHQiQSMQZS IxXQ== 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=UWnaWYwRxm004yuStCG/ymdD9adozZE82QyBwNLbSTa3yQy4gyiqPZoKR5zPXDjg1p r1j75Nv5Y/IQMrftAfeKPZ/ANwqWQlNDVCOZz3+V68lSH1wodETBBdxfi2ngRQgtxy2L /bEpYlSxmLcoUnC3RXiwxNjWpiS4XlYvhpVX6KC99ltYFe+/rpB/kbN7evW4xEKRW4A7 hsKoRHrI5I4F81NFfkAf2BhZaxERkm+38VBsWjjtwVLswM2daFl8vxYFNrpD4wrPScFW /M3IF1B4KKQ28fvnoTZNoMflp1Ybheu3Wo4iBvR2O8tljNNqpEA1hmp3NesJw6Pcke09 rAYQ== X-Gm-Message-State: AOAM530bQM01DRNlJtDZOpImT9S+q5l/wHu9+nUhwlL8SCe4HN9NivY/ tLkvEtQ6libkiFQyjmZnxeTZD4jkf9o= X-Google-Smtp-Source: ABdhPJxlXwqh5E7ZLIwjg7+HewldN0yZQdHDi7J60DpxZx4gKCVhIkOs9Sq/RDtjYQkDtF9t4MoTDA== X-Received: by 2002:a7b:ce06:: with SMTP id m6mr10517856wmc.85.1631536091522; Mon, 13 Sep 2021 05:28:11 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:11 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 10/21] trace-cmd library: Fix possible memory leak in read_event_files() Date: Mon, 13 Sep 2021 15:27:48 +0300 Message-Id: <20210913122759.3672494-11-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488709 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 A78FDC433EF for ; Mon, 13 Sep 2021 12:28:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8ED6860F6C for ; Mon, 13 Sep 2021 12:28:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240001AbhIMM3a (ORCPT ); Mon, 13 Sep 2021 08:29:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239998AbhIMM33 (ORCPT ); Mon, 13 Sep 2021 08:29:29 -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 D14BAC061574 for ; Mon, 13 Sep 2021 05:28:13 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id g16so14443924wrb.3 for ; Mon, 13 Sep 2021 05:28:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hIaSg/ThuS+rjVyXJEp0CTM6VFI1pB32pj1o7a+D8Jo=; b=CkMNXvOSbnvdxkwvcDpv+nBGOMoIclm2BUAkuWaNmTkgRIjl55YydJ/q5eF+WIJffC CBQ8qU7KoxV9IDzqL5oqAAlzTfyvQMAchWRpQLaKNLdEPq3rzh4NeSUVCYKYrs8byUMi 5FTV5baNsXLIipXRM/BUfdMyqfhYp7a0F+fVJPti7HvnXY44uZVqbzOCjbvRPdbtKihx 8rBKTMcMRp+80ZkyWh1sw4ZophfkUmqt077SmylLOeIzFkH7Vv/xxy8ac49vhJWNAck0 oQ/slZpzIWexoqETKFztEo7UyotpySVrJXdhImphvk/NpHvuVNffCThEb+glczuL7s62 Xp4w== 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=p2AxHu7tMm43BP8g3QuNHErSzMT8k2iN2bL1CLq4QGeFHWBgzSoUpcNxz1Pq2dpHdq MleUi7+dFYeZmBvJZ6+WLJXPxTqpfHx/c/OHHReH4SA3B8GNJIpa0gE2k/VC+c6XiSMt FlJJf0QWR6yaaMD5FeeICS/OkMiIlrfIgezi/Ye6253v3AZ9ovilPO65HHFPFVbvCzJL IBw9T4f6VTGJjJRv3l3EtM/W1i5bIdr+CuKVE7awsdVRt4mb7ugd4C1T+0ray1bQ9Q2l ORYo+FDpdYenf5HrRm1lV9/RsDgHB0RR8RwIE6O6pbZUk1mZmaO0HP/FP04HScXYDkkG Rc9g== X-Gm-Message-State: AOAM532mJVHQQyFJvNYEb83XDLTUuyVFM43QxLfuIux93Wc37lJ/A9R0 bn36iyYjcM2LPRqc+9oS270JaNeIXQw= X-Google-Smtp-Source: ABdhPJyYNSCg47Da6MUvrvWGm9un0FRVioyX90S4jIYBGhUNsqj2Te5kY9f03W9t832doRatEwT2ug== X-Received: by 2002:a5d:608e:: with SMTP id w14mr12330396wrt.18.1631536092517; Mon, 13 Sep 2021 05:28:12 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:12 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 11/21] trace-cmd library: Fix possible memory leak in read_proc_kallsyms() Date: Mon, 13 Sep 2021 15:27:49 +0300 Message-Id: <20210913122759.3672494-12-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488711 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 2788FC4332F for ; Mon, 13 Sep 2021 12:28:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0F65760F6D for ; Mon, 13 Sep 2021 12:28:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239998AbhIMM3b (ORCPT ); Mon, 13 Sep 2021 08:29:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240002AbhIMM3a (ORCPT ); Mon, 13 Sep 2021 08:29:30 -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 D6627C061574 for ; Mon, 13 Sep 2021 05:28:14 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id u19-20020a7bc053000000b002f8d045b2caso6474895wmc.1 for ; Mon, 13 Sep 2021 05:28:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WIhyREvMq7bERJU6rUAI/8YHOuVEpU7iUhUrHTuHcqg=; b=beCExSmvZqiUyFdodSTeB1Hqy5cVSJAzQV/lAoxGcdq3GK8n9scu3cz77M+9NMpYGg qBssfrMHQE8VYiMZLrjvAWSZNcd57jloGNcuOJV5QOnEorSHn4aC7xoQSuXGhzSRGo3Z 2bbAXrgduGJ5m7DCnoxP7ZBRY5dxzFVArueRZEFZrzx7qM6cesqxh3rYBsKf4Ohv6j8u gmhjxy5Ib3egaPzctS87kLg3Gv0hM19a6FpI+vf6JrD4vrtTflk6l8GaFcy165qQAtAU 3ht0EwzpjmB4je9+IG3URqBx3jCtl01pJ2z0KtzO+mch/PPdfM4RvviOtGluK0XrOd1E tZQQ== 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=euytKNUFxNti/05eRw5ZqV1AKKOpSiONK8hd/vZFC4+B89qyGEsLR+SdHvWFwJ6iCS VJeUlfxty681C0lh0ZzOCo/OqB6jaECWIEShOGdgWv0HhGz8dAXeTompTbu/lNHovZGV wKUu9LIyp64WE/Rnpo8sK9f91yxwksm9IsbBn1DA9yBk4WD8lQ1qqR+idcKTV3Jq0uiL bLPrV6BHBS30Q8vED+jF8NlSJ7D8D17bq/uOCfvVEyO8FjO8XQnwPX6lf0ozbWh1HmrZ 4+PnkjHdJHNeJF2FAoD/nfOT/NxnPJvOYWQDBQgH05UbUQyc/FpkEOb5CWuqTv9dbx+X +Mkg== X-Gm-Message-State: AOAM531nwF/825/XDF6CjhpCN7CBDRLtZIyhPU9DOK/mkfTl0Dg6X32/ lwlwTp0xV/KEvMZqka9nGO6vhWTMUGQ= X-Google-Smtp-Source: ABdhPJwpbdU6XsHWRoaH1FFeu4HqCPLvjEHWof9q6IH5UDGAyYYZXHKm4BRCUWX8csJuM0yG3FPRbw== X-Received: by 2002:a7b:c0d1:: with SMTP id s17mr11192065wmh.139.1631536093459; Mon, 13 Sep 2021 05:28:13 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:13 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 12/21] trace-cmd library: Fix possible memory leak in read_ftrace_printk() Date: Mon, 13 Sep 2021 15:27:50 +0300 Message-Id: <20210913122759.3672494-13-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488713 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 9BF2AC433EF for ; Mon, 13 Sep 2021 12:28:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7E59660F0F for ; Mon, 13 Sep 2021 12:28:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240003AbhIMM3c (ORCPT ); Mon, 13 Sep 2021 08:29:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240002AbhIMM3b (ORCPT ); Mon, 13 Sep 2021 08:29:31 -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 C71AEC061574 for ; Mon, 13 Sep 2021 05:28:15 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id d6so14389821wrc.11 for ; Mon, 13 Sep 2021 05:28:15 -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=XlIDzqG3pv0HpGcWNKHxFynKKupwP/6i5iNS0Fzars+SfNuj68psCN+ILynT1Am1cj dgokO5tMq41RN6/Lpg683lu8a/Y+R9NdDnIqpeoTlrNiwCTX5KY4nRaE3m0eqDp7Ugiy 8RFLttwGgSRotKy7yW3/YdeAndXlU40SfhfVteYyfcm0u9WmRm9q+Kf7ug/ST1uJKhoY kdzj+GjzZJyVAaDYD5mRNDC7UhIzg7voHrwgolkCD//PC6XhQmX66GXWqHtFNe5ViCIy JOIVgPvm025yTtXMaNOICRLrFUckObgloA48zSzaSwjeNLcZzatuM9UoSN7fu88VcDyw 6sCQ== 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=2R4PO4g+DNufUvqlVB1+2jM/jFNN78cJizOTiU87qCCbDJtldhxORFR5UCgdbx8IcT +0rlb3BMIcYH6/YEMr9nXhMSz3dfWvNsqBB3/0NcJeAKCZcde9DwU1C63IwaSWYLG6Sc Y5hgETcabjEfR5IUdgV9Nk+2OR26teZ7rmHqtFmw420rZ13wmnbXqJCUVt4+KGU/BI8N KEbzGM+UT2eccsgeRSk6eUdBHFTFo3zDicMUkqx2V4xrx2WUYVHHkRA8oo+NwcFDNjYr g3XcOp5LbczMv/eDxN+SQXfif+EqmuGuTgMo7JwjWgG86gIl5ltA3aYairTr+K5asNKk +Wog== X-Gm-Message-State: AOAM531+FXUh5pmPDCI6skn9gGX5BxF1evgazeBqVjaE4xFluGdODNwf pFfGjuhpXYOrj0qeriJ+/MhfpwLhOMQ= X-Google-Smtp-Source: ABdhPJx+LZQE7ewEQ4PrErQ1h13QGBHV9IJzdOIKYazQu6W5oDb38LpjEprMbBmE1RosWLjwRrM83w== X-Received: by 2002:adf:f48d:: with SMTP id l13mr6582705wro.94.1631536094492; Mon, 13 Sep 2021 05:28:14 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:14 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 13/21] trace-cmd library: Fix possible memory leak in read_and_parse_cmdlines() Date: Mon, 13 Sep 2021 15:27:51 +0300 Message-Id: <20210913122759.3672494-14-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488717 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 38F1CC433EF for ; Mon, 13 Sep 2021 12:28:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 22AA160F6C for ; Mon, 13 Sep 2021 12:28:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237447AbhIMM3g (ORCPT ); Mon, 13 Sep 2021 08:29:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236796AbhIMM3d (ORCPT ); Mon, 13 Sep 2021 08:29:33 -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 C344EC061574 for ; Mon, 13 Sep 2021 05:28:16 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id x6so14386290wrv.13 for ; Mon, 13 Sep 2021 05:28:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/A80fJWoSFzNxEk5fUNHia3MlkikAeaBVf8zLeooVqY=; b=PiifEJikGKMDJd4BbVJt6QP7VhXU2kbVq2u12qijvdcnXdIscnTaSrVMuu5wPUV8Lc d6xKbia+Lh6MKqeOq2ZzxLhQtBV+530Z+RyHyGESZqGf2JcCS5ulKcuFiDxSPxbPisYk hd9A51VgqYN5S8+1/ScFPk+e1uNp0oBnH860zHn5tefHTbEwIxi7q/1/q8ESTPk5fja7 2VhdUmAN4LlFqvQgcPI/Fo7mjdAVRNIrJIlYwW6K1nwwQjGXA9WbrbOz7rWK9MQLUSwj kYqj5+2aixF7jWMAWlnc+qyoPTZFt6YC0T3wYKfIazQCkuj+P1pprQVG5onX2WeLwGzn XkHw== 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=ky+jk1zvDTjXjN/jr/R8jF02/AWY2Z4+z4xaBPXGuolomO5p/OoQ9OqO+GO5Z79tap jOo+EY2G7U23LjxDFg4zQX1aR7S4cklUh4ZcR8jFBFlFe44lTqsiVXAWees3pUngxkmG OYKR9WvT/oQRDiFSarRobUuJ7+awNO3VYtvC9FJi8bpaDSZmp+yWwanVFFrq/LPnW6FQ SP/5b78Bor4WmhihT2V/5KqPxTZKyGwdf5Sa/h9y5RWS/jzKa/H/BgRxUIaldTQ8BRnR u5QwNyPZojHz8ILrlpTh7ODgcAZTdKzi31lY2Yczs/tGfKXyCeGVJKiNQUGDt0l6uH/F KEaw== X-Gm-Message-State: AOAM530+pYM2geqC9X8Vq9dSr2IFDzDXZ8Dw4SCOBEfoijWM0zEboY+8 3utT/9I45UXqjSNUIW5xCKwwrYIlmGs= X-Google-Smtp-Source: ABdhPJwpoH4RpeAF8EB13YdCCtyIscm7pgiNE2lPQBoWQciClwRzDY9tCn/J78v+9VbY+nOTpzTZFQ== X-Received: by 2002:a5d:6741:: with SMTP id l1mr12362725wrw.289.1631536095382; Mon, 13 Sep 2021 05:28:15 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:14 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 14/21] trace-cmd library: Set input handler default values in allocation function Date: Mon, 13 Sep 2021 15:27:52 +0300 Message-Id: <20210913122759.3672494-15-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488715 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 8DA0FC433F5 for ; Mon, 13 Sep 2021 12:28:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 759E360F0F for ; Mon, 13 Sep 2021 12:28:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240005AbhIMM3f (ORCPT ); Mon, 13 Sep 2021 08:29:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46612 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240002AbhIMM3d (ORCPT ); Mon, 13 Sep 2021 08:29:33 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8FE42C061760 for ; Mon, 13 Sep 2021 05:28:17 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id x6so14386362wrv.13 for ; Mon, 13 Sep 2021 05:28:17 -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=FHCAGCINTJlDQiO8/L2uHdnWhtmVQKJv58e7X3AQtyGOc36xgz9s3Yv5fvGHq262mj Zv1H28Rw0E4kZdhlx7EmBXV1puPQVSj53iMtI34VbcHTO9PUoF0p4QRPzcvVsHP45O1U x1l+fqNgUV16azWgPaj+kPC4HJ0aHL1KFjB33lfegNaemTABglSrAmu2FKixqK4uTwqV P7jm84RvD2KDKB7NPdXHoIL7LA6K0l6WBtGr6Fvv9KbR+YsPL3UK2k+FZZZEBdpmSvY8 Jui//iJCg7USqVidIjoKzoSsmN8ei9/TQcTd4ywUaqQRRe57EeQmVmfnaa8eVoaYZxAI kQXA== 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=Bn3CiRPTrgpsg98VkZgBfMS+85v2AYk6PJ4MSc7j4fYbjKr05bycwanZcUQKRFGs6Y Y2AQsRp1gzWNDAGSamxsghd+wmf8IK2oXArrH9LVZwEFbNwHnYTJeRwxkHUt/6e3aEPe 2kdqCs+cia91r0Cq/o/bjsB6fPnm+h3U4PGYQtTI6nJLDsLTK3v0j4DCyt7PtCJXJssn PXPawE8mNMmGilmD+zwxyvVYQ3zduup+kXBN72QgWOQELn+DLsafVWnte6uR1vp1t0i0 s9JcXQoCBk6BSsdZgl2s1yNfWBogbKLosPOSeHRO4QPuN3tDueRzBodYXvc9L42pHh/o VrJQ== X-Gm-Message-State: AOAM530yz/miG2887cG+CIKRA85XuJHHFQhIW+IbcBijoZdbGLs9pybz greBKnG+1Wc9vO4v8uRilpW+08tnn2k= X-Google-Smtp-Source: ABdhPJzaVtJn1IriRIjjFkFuyAa79TYCIzrmqap5Mco7DRGKSX5nWyWgXKcqI9VQA4Zp15LQKjYVbw== X-Received: by 2002:adf:f1c4:: with SMTP id z4mr12396282wro.418.1631536096166; Mon, 13 Sep 2021 05:28:16 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:15 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 15/21] trace-cmd library: Track maximum CPUs count in input handler Date: Mon, 13 Sep 2021 15:27:53 +0300 Message-Id: <20210913122759.3672494-16-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488721 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 86A16C433FE for ; Mon, 13 Sep 2021 12:28:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6C77C60555 for ; Mon, 13 Sep 2021 12:28:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240002AbhIMM3g (ORCPT ); Mon, 13 Sep 2021 08:29:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46618 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240004AbhIMM3e (ORCPT ); Mon, 13 Sep 2021 08:29:34 -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 B11CFC061762 for ; Mon, 13 Sep 2021 05:28:18 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id w29so13642456wra.8 for ; Mon, 13 Sep 2021 05:28:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=RL9NU2QU3eQCBHNiwR5XAbT/oZdzMq72Opi9smJDKmw=; b=HZcYUjWjC1oF1IweZeAFzFbHBP+6UMX9Qk+L7N2buzTdazO+Sf0/QIuVWAEL8MUQJ8 9ykz5LOtrOoprOUUmikobi1rAAdN92WIfQ0GgPPsYQvsccNIs0LLMxHZSoMbBMr3aO3A d64m3d0GuO0xr9XhuegEpzbLCy9ZrsrGr9KK5YievDrqt0ba+DMqEYK+3lu7TPAZa21d hmUro3ipl4qhMDZwGeV1rGgAazuta0V5icdvNrbUThgyMFKnfRCA38S8ehhXatOevRQE XMtKJ3Ii2VZiAEuDuJlixDJURM5F22BKdKFwBpDbjMMUyE/VKCs0ODf3yg0X/pvwaUCG ssSQ== 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=HCAWhANGnN10KOIW7kdD9jKcxjcsb6x1ecxrVmtvDTC1f2C/F0WCvPdnEwIhOkBThC 6f1o5WZX7vDfVDJPbx8jumwMy+/kd6Up73U+uZj7FVqY8MXiCRM8nUF+E5IHUWkOBL9o p0rhhuOPfZGvFiClp19O1AK9mz9mG9Ke8vTmbKF5PXkmgrpBmCaxKfvS5MXXSkNQSMdw zLnSFPEIU7x8K4bz1spNOC1A7E+TTyuSHwFI+DxkvnkDJ5CQhSi+YXw8JDbqXQC/lKvW +2wHyvb1N6wUl0RsYOaTIsuIKI8RAqkpgqtjE6/GlL2kjhG9Ij61Df1DZ2D7miDjgvGa yrkA== X-Gm-Message-State: AOAM532tsd80XSLnigfdfodlsnKISiZN7WJW1H2oKlKW+D6/Ly9L3h2u NsbO5OW0pWXW2D2R5W/fIUgQSOTdu1c= X-Google-Smtp-Source: ABdhPJyt01J0M6dgheowZchimRyq3d7bwO8PPoCa8pXezhysrcZJ14Br5gZ9YvE3pfBSoJHN3OfiGw== X-Received: by 2002:adf:cc82:: with SMTP id p2mr12562438wrj.5.1631536097311; Mon, 13 Sep 2021 05:28:17 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:16 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 16/21] trace-cmd report: Close input file handlers on exit Date: Mon, 13 Sep 2021 15:27:54 +0300 Message-Id: <20210913122759.3672494-17-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488719 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 CEB0DC4332F for ; Mon, 13 Sep 2021 12:28:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B2D5B60555 for ; Mon, 13 Sep 2021 12:28:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239946AbhIMM3g (ORCPT ); Mon, 13 Sep 2021 08:29:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240006AbhIMM3f (ORCPT ); Mon, 13 Sep 2021 08:29:35 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A1070C061760 for ; Mon, 13 Sep 2021 05:28:19 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id u15so8358031wru.6 for ; Mon, 13 Sep 2021 05:28:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=vFXaQI/G0HHdHdRSAHOmVE4YjkrR1Z9WT14pKr2Ncd0=; b=CTKCTJFiBM4XtajodQtQLZFImECHmaXCT6yZPsF/Wo8Z/oJdfAbWE+re3ZUt2iZI1d CQacv/0HKJWJVI0Ojz/7AALDNtkXi+h27lgcIkycj8BNDKFUK+65d3zvJkTDCXNjqAdA gztMG955AZoZ9AiAoyywuEGQzXmw2NVyyVUXT8kcBB1oPt2REa0z4f94u4JnLt+seqeH rqO6Tktwbxilwpo+0RtLj/4V5OcmPc2GZpPP8zR8zrtmPwYXKnfLte1wNmVMih9yBfVq c5NTq6LCY7oGCVLC3pUh7KB+kdz0zfH7JcPOO97R925xYcyfdj5b54mVDcP0xseEUUym IDfQ== 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=JiR44daP1EWj6JqfYPL5CyXlWIR1eC2jwgDB3yuAvYTOPrdMMffKBkhzSIyCeVrPlG cwF2QPVBhcZYdLzWv4Qk7NZVhhi2TtvY5iO6ZKq7SnhmqvxzMHtcHzC1/uTIp/FVPr4O T+tDrVvKJytoQ8tz/DSrLHu5GuX4a0tRhkxHdwE7VBmev+xwraentL22mZWq7aPmaECE UTdfKhdKGMZHvEkefMe+QdbHcVZazzf2R6aZaBJuFbL17RtV1AWlPMI6jACuRhM0H/dD HV1dGg8d60PRJRRAVPrNGF1WXMKG7gurFex1qu8w3NyX63Tvrq8j7M4NyzsSdX5Wcysk aSow== X-Gm-Message-State: AOAM5319AvPqTRg9puJH4/TwjoJS3xblB3TGrk8cDVHumUWm3vSIwLdW 6ipIY7Q+tE9cHRvlbccKgMw= X-Google-Smtp-Source: ABdhPJzqxqyRx+73yYSq6MvfbH2EP3rkkbBBXNpii/aOXjW6Y5SczDojY61r3LE4i4bj+uFCxijA5g== X-Received: by 2002:a5d:58c9:: with SMTP id o9mr12466951wrf.279.1631536098277; Mon, 13 Sep 2021 05:28:18 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:17 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 17/21] trace-cmd report: Do not print empty buffer name Date: Mon, 13 Sep 2021 15:27:55 +0300 Message-Id: <20210913122759.3672494-18-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488723 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 585DAC433F5 for ; Mon, 13 Sep 2021 12:28:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3F17460F0F for ; Mon, 13 Sep 2021 12:28:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239963AbhIMM3h (ORCPT ); Mon, 13 Sep 2021 08:29:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236796AbhIMM3g (ORCPT ); Mon, 13 Sep 2021 08:29:36 -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 92829C061574 for ; Mon, 13 Sep 2021 05:28:20 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id v20-20020a1cf714000000b002e71f4d2026so5137051wmh.1 for ; Mon, 13 Sep 2021 05:28:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=AA+PGUMl91DR7kV9NuJ9YoaOinBRBGDhoRdn94IqH5o=; b=aCJE9HYHxqab9cjnWVGsCnQJE1Qe4YUgtnwPo++62fEvWanzvlutORwOc53mO2M25k mMnkO26zyu9cR/R2Ginty0tO4EJm3AmCH1J6qBz2yNVGSIWjQ+xuZ5owPU2OW6D80eEN 0qsDjFmZmvBzY9GxEhBmut8er/2BjuCGPtJdMv67CnFDEv3w/GWpZiQNvhW4bZho2ooq diJG9Z5BEx7xcTdMTsiKWIDmrrkNm624hO8xRuBCv/4l00n4ipqFr9X4K4zHyzBn1jps TrbhMcSYWjr5bsRyBaX1nSMAXqH9wLpiVlDqztCXJ5rbhKMCvBmKYaHmc2IwxAzwuU4W pKlw== 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=lIf3xiyXPpyM6uxJXbDOPw5gMoT1zGzAxnEJvltWCWwXR1X/jz80tjsEZIc8acLzam oUp8T3u0f3zTyRDvM+uAbg+lBLX2MFHZlrkgnlU48j5H+k4zRfycg36V9Oy8xG9zDRJM p6sVOeK9IomYSiPI3rH7quugbdSuWTedVzKDVg9mVWLqaQKcZfOyVXn5U85x29GwloLt J5lFhzEXrQJwS6x0iju/A6VJ23Ct8klk+TFVOiUUTHNr8+0pvXVgA6jSozhnSwF5li+g ycHlsWaQlim+RNgWFnTYreo/slKRljznjlR5mH9/yC39A1vrK2MLi7vnsz8tkNn8lzuZ qbWA== X-Gm-Message-State: AOAM531juru9+IJNiEi7ab8jGGtSWbwQXYGcQAw0WMX0fd3mvumgwtle kNQ9Cde1WyeQl+48CplQoaGSvKmZn8A= X-Google-Smtp-Source: ABdhPJwbkbXms5bs9xuSPGqg8kaNcpbcWwxUNhAykqNKx/Hb1tM5G1WTRds6fev/DId8XvA0pIqBLw== X-Received: by 2002:a05:600c:3585:: with SMTP id p5mr1159748wmq.110.1631536099221; Mon, 13 Sep 2021 05:28:19 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:18 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 18/21] trace-cmd library: Set correct CPU to the record, retrieved with tracecmd_peek_data Date: Mon, 13 Sep 2021 15:27:56 +0300 Message-Id: <20210913122759.3672494-19-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12488725 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 65AE5C433EF for ; Mon, 13 Sep 2021 12:28:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4FEBC60555 for ; Mon, 13 Sep 2021 12:28:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240004AbhIMM3i (ORCPT ); Mon, 13 Sep 2021 08:29:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46634 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236796AbhIMM3h (ORCPT ); Mon, 13 Sep 2021 08:29:37 -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 BD3F1C061574 for ; Mon, 13 Sep 2021 05:28:21 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id g16so14444535wrb.3 for ; Mon, 13 Sep 2021 05:28:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8Zt0veOqDDSXpzCVbb+xlRgowJ7dD1XDzcMmVcCpCxA=; b=Y770IVQa7nOWLaRWQGIpZ8kWrk93tRzQcMOuiMSLrJY69w/GuQd3Kh+J8h3mIq8gbl TB7sPs9XZS2sLo2a0gQkug4WY7vDN+rBh6KSLesfDqk4MurpKFqxkm6PWK3YUdJR13Hb 511NbKRe2dB0hElizJ192Wa/sEKYlrgxbUsbeOVqRz4E7LrCf7T32IecErGVTos8k8og HnMdXHSG4jt9gQFUDUr1ugi2R7Pd6OZdEoCMXnFpR+xrQstduI+LM2C3whW07CRdz8+a KTvHvQE9vHA/rEaRTzo9gSaILbja+0DiI+TSxZQOhbjWqJ+XdNqHJ+PBoqG6R2/fYTC4 jplA== 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=8CVgAtA02eEfTZLKGZvvzOFwQGWgOTbXkRfSMgXnXKBtahBKiqSlLZCi+Jhj401GOT XHE2eBh92s4afjOx7Bqge7gNDC+CIwWNOnCzJG7N/LsPhLAijo8g0Cd2IwrnTHD7+UCJ 6U66697OYmX+B7BdMuAXlwMEG+bAUiV0OdhJO0wIDZAlAD0Xwxj+/v6RGaTTbe/TPSst nYJNCPBxmiLtkaGPYTYN/Hefj6XbNjJeJ2tIo3uM/ydENoN0A3WU4QSfFy9B2VToDq05 4yaVA43qy2hvvTjp4HsBX2Cwu9hggnUBM7h5e5K0AlXJuZol2Ak7wewvaDWgfMNgZI/v Vs6g== X-Gm-Message-State: AOAM531qGSsl9pG5mHg2OvVrreScVHIPG8xfgUWw7D84XosUqCueprzO 6iFgNALVQVRNSpSJVm3Wu72i5tzSMsc= X-Google-Smtp-Source: ABdhPJzeJ+bBRS4ORsIHhcpnmb8v5SD7UtzKBZWcpCdE7+FnN5Xu6TC14zpWW8npvPGlwAhqiMUjSw== X-Received: by 2002:a5d:650b:: with SMTP id x11mr12136433wru.350.1631536100205; Mon, 13 Sep 2021 05:28:20 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:19 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 19/21] trace-cmd library: Refactor APIs for creating output handler Date: Mon, 13 Sep 2021 15:27:57 +0300 Message-Id: <20210913122759.3672494-20-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 Mon Sep 13 12:27:58 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: 12488729 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 083A5C433FE for ; Mon, 13 Sep 2021 12:28:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E276560F6C for ; Mon, 13 Sep 2021 12:28:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240009AbhIMM3j (ORCPT ); Mon, 13 Sep 2021 08:29:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236796AbhIMM3i (ORCPT ); Mon, 13 Sep 2021 08:29:38 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2D7CC061574 for ; Mon, 13 Sep 2021 05:28:22 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id j17-20020a05600c1c1100b002e754875260so6470194wms.4 for ; Mon, 13 Sep 2021 05:28:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/PnWcaDwPhsPDbAnngZjAKfnpEK7t7OBUsV7BkAfTWg=; b=oX+KQJW8GWDATiA9cNwBcwhKBUFiEr8xoPy5u1/dRe5QSlPO4Fo7giPXfFZQPL3oU/ iVv+VE8l2vMdq+oXTO6Q58eHVSj4V6OsmswOdf5uEKftu8Pr9BawBPn+cwm8rYHFf1Mv kVZLJzP/SaQjYvwj8Q68aP/52yVDi+37CDAbFmZwUN5WPoDDPKj1t2qz8MEazptgvAOR NxKSkpChZvj2vFgR+jPiA4MDx1Mkz4Ml3tXtoXwRAZWr5t2twT0e72nC0x1Hst83L50c Y8LkL+6xI6AXkTZJi7LzV/uRkwxCwMv7wU95EsV70VeIHsIwUvYNIGE8TwapbMJXRBcN pwPw== 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=/PnWcaDwPhsPDbAnngZjAKfnpEK7t7OBUsV7BkAfTWg=; b=17vNbMi0AI0qLTSCayzw2v0Jo7BZJA6aPYaiKHWt9/9kj21/w1P2YNYGPi6HPiqFZf nCvXZU8+05DHUo/ZhqnivSHTnSDhddsef8t13jpIgbKNvZsOucrqiAMuQA4m+EqM/ugV LpJU0u4Uu3EQCCd1K7q+nWBhZL5Yn/wpncX8uTiH71YFF3DC8XdvW0LnhJSfRAYwmeTw peBNcMemZ/eyY8/sO9AKy+doOD3N08PLpTRah7sg2FhZN/qeLAPeleht0dY4nAhDliUB JPQ+yhRR+nzGTByFGkVig5vkF5sKnlct3PQG16tJEFHc0En0IPciIJxrPDC5DkInN8YC tqJA== X-Gm-Message-State: AOAM531UHhfxzmFL889qw88+iLfcT8goMznCYJCOa8MRJPdhFpEDw6jE XXt/pDraoO1Ib9hbnwVkJbacnzDMh3s= X-Google-Smtp-Source: ABdhPJyPGoz6NfsRuau8SZwZ3tquJMMaR2SszClqBV3Gu1aZ2zuKzL5ZEERmu+3pwU0UVY4dSdrBKA== X-Received: by 2002:a05:600c:3554:: with SMTP id i20mr10709281wmq.164.1631536101287; Mon, 13 Sep 2021 05:28:21 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:20 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 20/21] trace-cmd library: Refactor the logic for writing trace data in the file Date: Mon, 13 Sep 2021 15:27:58 +0300 Message-Id: <20210913122759.3672494-21-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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 | 305 ++++++++++++------ tracecmd/trace-listen.c | 2 +- tracecmd/trace-record.c | 18 +- 5 files changed, 234 insertions(+), 118 deletions(-) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 167f3804..97cd82f8 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -290,8 +290,8 @@ struct tracecmd_option * tracecmd_add_option_v(struct tracecmd_output *handle, unsigned short id, const struct iovec *vector, int count); -struct tracecmd_option *tracecmd_add_buffer_option(struct tracecmd_output *handle, - const char *name, int cpus); +int tracecmd_add_buffer_info(struct tracecmd_output *handle, const char *name, int cpus); +int tracecmd_write_buffer_info(struct tracecmd_output *handle); int tracecmd_write_cpus(struct tracecmd_output *handle, int cpus); int tracecmd_write_cmdlines(struct tracecmd_output *handle); @@ -303,13 +303,11 @@ struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, const char *file); int tracecmd_write_cpu_data(struct tracecmd_output *handle, - int cpus, char * const *cpu_data_files); + int cpus, char * const *cpu_data_files, const char *buff_name); int tracecmd_append_cpu_data(struct tracecmd_output *handle, int cpus, char * const *cpu_data_files); int tracecmd_append_buffer_cpu_data(struct tracecmd_output *handle, - struct tracecmd_option *option, - int cpus, char * const *cpu_data_files); - + const char *name, int cpus, char * const *cpu_data_files); struct tracecmd_output *tracecmd_get_output_handle_fd(int fd); /* --- Reading the Fly Recorder Trace --- */ diff --git a/lib/trace-cmd/include/trace-cmd-local.h b/lib/trace-cmd/include/trace-cmd-local.h index 7f3b45a8..547ee5c3 100644 --- a/lib/trace-cmd/include/trace-cmd-local.h +++ b/lib/trace-cmd/include/trace-cmd-local.h @@ -31,7 +31,24 @@ void tracecmd_info(const char *fmt, ...); #endif #endif +struct data_file_write { + unsigned long long file_size; + unsigned long long write_size; + unsigned long long soffset; + unsigned long long data_offset; + unsigned long long doffset; +}; + bool check_file_state(unsigned long file_version, int current_state, int new_state); bool check_out_state(struct tracecmd_output *handle, int new_state); +struct cpu_data_source { + int fd; + int size; + off64_t offset; +}; + +int out_write_cpu_data(struct tracecmd_output *handle, int cpus, + struct cpu_data_source *data, const char *buff_name); + #endif /* _TRACE_CMD_LOCAL_H */ diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index 20116d99..59f0e245 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) - goto out_free; - sizes = malloc(sizeof(*sizes) * cpus); - if (!sizes) - goto out_free; - - offset = lseek64(handle->fd, 0, SEEK_CUR); - - /* hold any extra data for data */ - offset += cpus * (16); - - /* - * Unfortunately, the trace_clock data was placed after the - * cpu data, and wasn't accounted for with the offsets. - * We need to save room for the trace_clock file. This means - * we need to find the size of it before we define the final - * offsets. - */ - clock = get_clock(handle); - if (!clock) + data_files = calloc(cpus, sizeof(struct data_file_write)); + if (!data_files) goto out_free; - /* Save room for storing the size */ - offset += 8; - offset += strlen(clock); - /* 2 bytes for [] around the clock */ - offset += 2; - - /* Page align offset */ - offset = (offset + (handle->page_size - 1)) & ~(handle->page_size - 1); for (i = 0; i < cpus; i++) { - file = cpu_data_files[i]; - ret = stat(file, &st); - if (ret < 0) { - tracecmd_warning("can not stat '%s'", file); - goto out_free; - } - offsets[i] = offset; - sizes[i] = st.st_size; - offset += st.st_size; - offset = (offset + (handle->page_size - 1)) & ~(handle->page_size - 1); - - endian8 = convert_endian_8(handle, offsets[i]); + data_files[i].file_size = data[i].size; + /* Write 0 for trace data offset and size and store offsets of these fields */ + endian8 = 0; + data_files[i].doffset = lseek64(handle->fd, 0, SEEK_CUR); if (do_write_check(handle, &endian8, 8)) goto out_free; - endian8 = convert_endian_8(handle, sizes[i]); + data_files[i].soffset = lseek64(handle->fd, 0, SEEK_CUR); if (do_write_check(handle, &endian8, 8)) goto out_free; } - if (save_clock(handle, clock)) + update_buffer_cpu_offset(handle, buff_name, data_offs); + clock = get_clock(handle); + if (clock && save_clock(handle, clock)) goto out_free; - for (i = 0; i < cpus; i++) { + data_files[i].data_offset = lseek64(handle->fd, 0, SEEK_CUR); + /* Page align offset */ + data_files[i].data_offset = (data_files[i].data_offset + (handle->page_size - 1)) & ~(handle->page_size - 1); + data_files[i].data_offset = lseek64(handle->fd, data_files[i].data_offset, SEEK_SET); + if (data_files[i].data_offset == (off64_t)-1) + goto out_free; if (!tracecmd_get_quiet(handle)) fprintf(stderr, "CPU%d data recorded at offset=0x%llx\n", - i, (unsigned long long) offsets[i]); - offset = lseek64(handle->fd, offsets[i], SEEK_SET); - if (offset == (off64_t)-1) { - tracecmd_warning("could not seek to %lld\n", offsets[i]); + i, (unsigned long long) data_files[i].data_offset); + if (lseek64(data[i].fd, data[i].offset, SEEK_SET) == (off64_t)-1) goto out_free; + if (data[i].size) { + read_size = copy_file_fd(handle, data[i].fd); + if (read_size != data_files[i].file_size) { + errno = EINVAL; + tracecmd_warning("did not match size of %lld to %lld", + read_size, data_files[i].file_size); + goto out_free; + } + data_files[i].write_size = read_size; + } else { + data_files[i].write_size = 0; } - check_size = copy_file(handle, cpu_data_files[i]); - if (check_size != sizes[i]) { - errno = EINVAL; - tracecmd_warning("did not match size of %lld to %lld", - check_size, sizes[i]); + + /* Write the real CPU data offset in the file */ + if (lseek64(handle->fd, data_files[i].doffset, SEEK_SET) == (off64_t)-1) + goto out_free; + endian8 = convert_endian_8(handle, data_files[i].data_offset); + if (do_write_check(handle, &endian8, 8)) + goto out_free; + /* Write the real CPU data size in the file */ + if (lseek64(handle->fd, data_files[i].soffset, SEEK_SET) == (off64_t)-1) + goto out_free; + endian8 = convert_endian_8(handle, data_files[i].write_size); + if (do_write_check(handle, &endian8, 8)) + goto out_free; + offset = data_files[i].data_offset + data_files[i].write_size; + if (lseek64(handle->fd, offset, SEEK_SET) == (off64_t)-1) goto out_free; - } if (!tracecmd_get_quiet(handle)) fprintf(stderr, " %llu bytes in size\n", - (unsigned long long)check_size); + (unsigned long long)data_files[i].write_size); } - free(offsets); - free(sizes); + if (lseek64(handle->fd, 0, SEEK_END) == (off64_t)-1) + goto out_free; + free(data_files); handle->file_state = TRACECMD_FILE_CPU_FLYRECORD; return 0; out_free: - free(offsets); - free(sizes); + lseek64(handle->fd, 0, SEEK_END); + free(data_files); return -1; } +int tracecmd_write_cpu_data(struct tracecmd_output *handle, + int cpus, char * const *cpu_data_files, const char *buff_name) +{ + struct cpu_data_source *data; + struct stat st; + int size = 0; + int ret; + int i; + + data = calloc(cpus, sizeof(struct cpu_data_source)); + if (!data) + return -1; + for (i = 0; i < cpus; i++) + data[i].fd = -1; + for (i = 0; i < cpus; i++) { + ret = stat(cpu_data_files[i], &st); + if (ret < 0) { + tracecmd_warning("can not stat '%s'", cpu_data_files[i]); + break; + } + data[i].fd = open(cpu_data_files[i], O_RDONLY); + if (data[i].fd < 0) { + tracecmd_warning("Can't read '%s'", data[i].fd); + break; + } + + data[i].size = st.st_size; + data[i].offset = 0; + size += st.st_size; + } + + if (i < cpus) + ret = -1; + else + ret = out_write_cpu_data(handle, cpus, data, buff_name); + + for (i = 0; i < cpus; i++) { + if (data[i].fd >= 0) + close(data[i].fd); + } + free(data); + return ret; +} + int tracecmd_append_cpu_data(struct tracecmd_output *handle, int cpus, char * const *cpu_data_files) { @@ -1589,41 +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 Mon Sep 13 12:27:59 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: 12488727 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 8E8F0C433F5 for ; Mon, 13 Sep 2021 12:28:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7709960555 for ; Mon, 13 Sep 2021 12:28:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240006AbhIMM3k (ORCPT ); Mon, 13 Sep 2021 08:29:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240008AbhIMM3j (ORCPT ); Mon, 13 Sep 2021 08:29:39 -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 ABC2EC061760 for ; Mon, 13 Sep 2021 05:28:23 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id b21-20020a1c8015000000b003049690d882so3992468wmd.5 for ; Mon, 13 Sep 2021 05:28:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rgKSGpz75JPloOaC/1DcOk+gg+XSb9yeocHeU0wjVV0=; b=H79l7xWdS6wROyXk9T/okuK6E0MvVIf21WGllpDWL+PxAO8ROZt0ALb7yWhvx0A+Bl TyLczSAABpAT4kG2uU8u9eWkMKUBLROPwg3LACGUA65oI+/rGIVMOwcBkAhWd0cB+6D1 zwskiqyuU2mU9jP9Hj8ijtKhqSpKxZ7KUcJp9StQ3AX5ba+CT+TFSbFtp9GZHaycP3+H j3qRzk38peh1vb/HcQ+8Wxci+uHPMcq/VOHGHKi7wdGdchR/uWD0RW8H1MFT7JyVLs2c QnpNfv4KCu0+Q+GbDIHcNyjutJfCRGUCghj7wt6oXOA6a7LJ4c15mvAw+gRZFIXGpbXL 6Fug== 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=3Qfaf8Wc1RZ495G3lX+EfSRsQnYvg3AUnoEdJTZiU3GUOLDtiUcNiC7iJ5fo816iLX DQhX5Pq/cdK/3Ape34ifMXBlBieWgFjGm8D5DthTuGgX35WgHyD8Dff44efUIAeRqbGY elwAnL4dcr+71aA2TMaHZ7oHr3NtATG4NhW4dM/V5mIa0EvMtT2BB/roqxN8W52vmkCJ 5hCMff/C0n6N7/hWyDNjLM/oaFyU/5dBmgYtkGy1LWKTCpVzrHsl+OOpI08q7IoeNx49 nrTBOAv44k8fEfxWmlh058j4vTjLEOmVqYFqj8uQN7dsbK7zOqSR7vNWic9j9Z4UR8mq Rhkg== X-Gm-Message-State: AOAM532HHtK1YdjQaRvYfzFro4RtGShNjb0wSDdn6WGSwXPd+1KPv0f6 5hgLo9GbA0Ndp9xWKxZfPX4= X-Google-Smtp-Source: ABdhPJzAS0eiS4t9i2BK2S6AZuMBB7fJTiU/n2WC/QWaUWERm0vxTocAw+03iOoMc8q3AnNH5MeI8w== X-Received: by 2002:a05:600c:4f52:: with SMTP id m18mr10738905wmq.34.1631536102276; Mon, 13 Sep 2021 05:28:22 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z6sm6839689wmp.1.2021.09.13.05.28.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Sep 2021 05:28:21 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 21/21] trace-cmd report: Init the top trace instance earlier Date: Mon, 13 Sep 2021 15:27:59 +0300 Message-Id: <20210913122759.3672494-22-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210913122759.3672494-1-tz.stoyanov@gmail.com> References: <20210913122759.3672494-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)