From patchwork Thu Nov 11 15:03:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12614981 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DD1F4C433FE for ; Thu, 11 Nov 2021 15:03:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C4BB061390 for ; Thu, 11 Nov 2021 15:03:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233798AbhKKPGA (ORCPT ); Thu, 11 Nov 2021 10:06:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232823AbhKKPF7 (ORCPT ); Thu, 11 Nov 2021 10:05:59 -0500 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82C14C061766 for ; Thu, 11 Nov 2021 07:03:10 -0800 (PST) Received: by mail-ed1-x536.google.com with SMTP id m14so25634499edd.0 for ; Thu, 11 Nov 2021 07:03:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=1CAu8is3X6uqY86zrUEK3QM/ifPbXC7s1X/vTycEMbo=; b=XH+InOS/iP0Y/L0yb+pKFtj8Va6ZViccbUO5PPx3fRMRBff/dv+kRBzkHaMxD+GYx6 6tHJT9iZHJzzP/aCFw9WB1QwMM77jR1XCgvXXZtHaddxHovp4g0v7QpzBH25CHsnxWdB /HOlhJbshp7ZHvHS03afvp+Mos4PUPlGS4gQm1H/F0laHwPJEe72vmygu4CFpIabVDYk d0olH8ua8cmS4ZnYpamAskXsc1r5UOSRSrJB16iTDDqGz7sziPQKGMeqKK9cMRGW/1Uh 2ts8QSPe59MLr9/dJeDQ/mhSrBVFh/bKlhyKeGijN2n7PAwLAEXzKRBWZWdrG3A8vdrQ Zt7Q== 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=1CAu8is3X6uqY86zrUEK3QM/ifPbXC7s1X/vTycEMbo=; b=OiUZqAddn2hsltfw82DY4GauOpUNkQlT5XCmJtYgQpSwROSc+svH9kqFfn+mK4LU+Y 9bOGIa6BU8DR4EZr+HlQ5mPww8q7fZuBfq5iByIKlZmztjFcURS0iDZ32h/GQWnfDNSr 1epJNqoHgNVls1mkKahI88weiFY1KR3VmobAgLrx5v+KJ8aqCSm6mySspHz2Pb1cFLJ8 +NjENxpstdGhKcnGbFdInOlLdDbp/gsyHvzpCzDLxzNdd8Q7/p7wCJ/kC37pDs1AMxgW 3lq7jBd98WL1UnVqORth8+/622HThlcoQsO2CV9DJVjOg8QxwDGDxFIORuxCd05B5F79 TbPg== X-Gm-Message-State: AOAM532pTxaQNNepd/vmELuObbTU6KHw9SXOFwuE1ffyZrpBZXRYFPl9 cyGA8MYr/o85rd8zex0MItoviRhoZSDeHw== X-Google-Smtp-Source: ABdhPJw2k2Fz+22+2CActZHWod2DKVtUqRV1aOWw0DGGJjuLpRCMzEBM7LQ2ZbpFmoi58hssDmiqUg== X-Received: by 2002:a05:6402:28e:: with SMTP id l14mr10609096edv.162.1636642989099; Thu, 11 Nov 2021 07:03:09 -0800 (PST) Received: from oberon.zico.biz.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id w3sm1737826edj.63.2021.11.11.07.03.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Nov 2021 07:03:08 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v5 1/6] trace-cmd library: Do not use local variables when reading CPU stat option Date: Thu, 11 Nov 2021 17:03:00 +0200 Message-Id: <20211111150305.85943-2-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211111150305.85943-1-tz.stoyanov@gmail.com> References: <20211111150305.85943-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 | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index df2e42bd..e890f246 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -133,6 +133,7 @@ struct tracecmd_input { bool read_page; bool use_pipe; int file_version; + unsigned int cpustats_size; struct cpu_data *cpu_data; long long ts_offset; struct tsc2nsec tsc_calc; @@ -2653,7 +2654,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; @@ -2735,12 +2735,14 @@ static int handle_options(struct tracecmd_input *handle) break; case TRACECMD_OPTION_CPUSTAT: buf[size-1] = '\n'; - cpustats = realloc(cpustats, cpustats_size + size + 1); + cpustats = realloc(handle->cpustats, + handle->cpustats_size + size + 1); if (!cpustats) return -ENOMEM; - memcpy(cpustats + cpustats_size, buf, size); - cpustats_size += size; - cpustats[cpustats_size] = 0; + 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 */ @@ -2810,8 +2812,6 @@ static int handle_options(struct tracecmd_input *handle) } - handle->cpustats = cpustats; - return 0; } From patchwork Thu Nov 11 15:03:01 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: 12614983 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 995A9C433F5 for ; Thu, 11 Nov 2021 15:03:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7BC3F61284 for ; Thu, 11 Nov 2021 15:03:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233817AbhKKPGB (ORCPT ); Thu, 11 Nov 2021 10:06:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233818AbhKKPGB (ORCPT ); Thu, 11 Nov 2021 10:06:01 -0500 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA7F7C061767 for ; Thu, 11 Nov 2021 07:03:11 -0800 (PST) Received: by mail-ed1-x52d.google.com with SMTP id w1so25363470edd.10 for ; Thu, 11 Nov 2021 07:03:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=FjyGq3J6UVgmNzFWTIa2KGHfOJEWj12W0YtEc1ZQcrs=; b=itENR7gcLMggFKrIlzO+Peio7KKivljhFQCq/X+Z8beOH+PrrC64VTxqJLUs7cFla4 Y3VxPaa9+1tcVDmAJzDongQOVhPHJ5E30SsvxtCeMoEcrtPTia3Ddm5Z9PBJafqM4UFw mnd/UtzGZa+VRbQ9L1norXXrmBWNyOrrnra6YANwBTBGZfSMq9ojvNgby0EwDDn5WdfI sJAp+4RPcMaj6MUMR7cjUi4rvjahGs3YdEFK8Nfn1XzgVj/R7NyeOhOZXge6/6yUAzLS HadasztDXlvwUqoN66FBvwO500+Jj81yqn3BkQUlfPy7Gn4R3OiBUvqSkmtf60u0jAil waZg== 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=FjyGq3J6UVgmNzFWTIa2KGHfOJEWj12W0YtEc1ZQcrs=; b=tfesmsvqGfrNI77aQhMCUUgoXlwEDvKKH32m2KYSaAL9dNFBzgGTuVP1OP+GEAVJ7g A0AfwPaw5KguvvDgAsbguEAB8T17JpJHxt28xYT0rE6mmA7ebOW6fkkD0e8eBA+/JU8T hTgznvUT7yf3SrMj8oVsIy8qcNizO7DNlbwBU8igd7n9WZlBbaDzyuY44JVqic1oixum fRo/TnFnCbgw4hPmH0vSWIfRaiswttFihhO2ZeSYDr0OZebktRHM+RkQ5EVWZHz5Dfff YT/W4JGDd/2qtM7K24SmvIFFPfm3jSWjYxPYfnTp29IXc3zOmRcoxrRtBlePFqJJL4ho NzEw== X-Gm-Message-State: AOAM5312APaFtasMZJDLXj+pJZdinI7V7yonYq8yn88sp5AsOSWv7G5P 03UqnAS2pVBhDg6+cbvF/2aHXrHlkjsrmw== X-Google-Smtp-Source: ABdhPJwQIata68S07aWxnLusmIqP5U/pX+UTTJGbUIAGd98TcBfIOhpyng5/JRy3VIa6J1zS1JzWyw== X-Received: by 2002:a17:907:6e10:: with SMTP id sd16mr10537735ejc.158.1636642990396; Thu, 11 Nov 2021 07:03:10 -0800 (PST) Received: from oberon.zico.biz.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id w3sm1737826edj.63.2021.11.11.07.03.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Nov 2021 07:03:09 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v5 2/6] trace-cmd library: Track maximum CPUs count in input handler Date: Thu, 11 Nov 2021 17:03:01 +0200 Message-Id: <20211111150305.85943-3-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211111150305.85943-1-tz.stoyanov@gmail.com> References: <20211111150305.85943-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 | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index e890f246..2b936ddc 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -126,6 +126,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 */ @@ -827,6 +828,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; @@ -2779,6 +2781,9 @@ static int handle_options(struct tracecmd_input *handle) case TRACECMD_OPTION_CPUCOUNT: cpus = *(int *)buf; handle->cpus = tep_read_number(handle->pevent, &cpus, 4); + if (handle->cpus > handle->max_cpu) + handle->max_cpu = handle->cpus; + tep_set_cpus(handle->pevent, handle->cpus); break; case TRACECMD_OPTION_PROCMAPS: if (buf[size-1] == '\0') @@ -4053,7 +4058,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 Thu Nov 11 15:03:02 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: 12614985 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1D1E7C433EF for ; Thu, 11 Nov 2021 15:03:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0954161354 for ; Thu, 11 Nov 2021 15:03:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233818AbhKKPGC (ORCPT ); Thu, 11 Nov 2021 10:06:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35954 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232823AbhKKPGC (ORCPT ); Thu, 11 Nov 2021 10:06:02 -0500 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE4B2C061766 for ; Thu, 11 Nov 2021 07:03:12 -0800 (PST) Received: by mail-ed1-x535.google.com with SMTP id b15so25116718edd.7 for ; Thu, 11 Nov 2021 07:03:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ElJcGpfmhsho78B9+WLz1UILTdptIZq/XBsK8PQj/3I=; b=G8ckgeN9S7SoSeWCUvKzZ6MJznBC56RXc+mqJSxpnia3Y7hQJLxLiO8kroARExWeJz 9wDFS72AxZ93C8go1FYyRKNDCxw7jAx9gTA8MIVABBgPaj3et/3n1lTprWImSj3IvCq8 BT8/d+jNF0FmUbZaTNImQplFYrlvbnWZNW4eHCzT2fZM4KDACdEhspabQt3U+PcLZKuZ Dzunc1kYfWOmyJsI1UkcHybIL47Y6y7vAQxtd1A+Nu4CckUjquKHquU/BlNUzwa9Fs0y 9zT8GwrZjdx8IrsF3uK8BCvHIgE+WsuvlGwIlbt9XTjJxnkBvmAs3Nu1jURQRrrhoh5O zlyQ== 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=ElJcGpfmhsho78B9+WLz1UILTdptIZq/XBsK8PQj/3I=; b=YfKSkMbXzHL/eDjberVkS1/TYWhxA02EbKkYtXDZI/6RQ5BYz05uNagw2FKmxcBSMu LM7HkVyH7Es4a77LEI1mWfOFJ3PnjsyN/yhPtNT6s7Y2IQdZAlOjw9CDZuMY0OU+H3KS fkZ7z/BwK/sAXa5fvVrvM4uAKm8lS0b/0gDynZ9/DWXzLaOCvHmkw9cz3Oj6rqOgk28c J2qfRli02u6baQk9nO/E0gwnlaNWK2NCeY4V0JPqa/qClNDgDpkHs+5EKaxu/3ydVaXr lVZ3AXrUp8DF/PT9feoVOpSdbGTOWuwLttWO3SOBErLREd9F8wQQWZi8jg9V7IWXhRmj qkAQ== X-Gm-Message-State: AOAM530ommAFBCO5qxHCyL6Z5wlyuqtnfD4RK3Snf0eWphml7REg437y sizw4jFZVBm/2joYs1rWTB4= X-Google-Smtp-Source: ABdhPJzbC7pgKMVtcKb2SsdWt847dtjRLl+fUvWGNz1nZrZBpE++wreA4sWz+58vkG9OuS4BhEaxEA== X-Received: by 2002:a05:6402:557:: with SMTP id i23mr10802189edx.176.1636642991372; Thu, 11 Nov 2021 07:03:11 -0800 (PST) Received: from oberon.zico.biz.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id w3sm1737826edj.63.2021.11.11.07.03.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Nov 2021 07:03:10 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v5 3/6] trace-cmd library: Fix possible memory leak in read_event_files() Date: Thu, 11 Nov 2021 17:03:02 +0200 Message-Id: <20211111150305.85943-4-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211111150305.85943-1-tz.stoyanov@gmail.com> References: <20211111150305.85943-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 2b936ddc..f9dd0784 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -645,7 +645,7 @@ static int read_ftrace_files(struct tracecmd_input *handle, const char *regex) 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 Thu Nov 11 15:03:03 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: 12614987 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5F21EC433F5 for ; Thu, 11 Nov 2021 15:03:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4B72261284 for ; Thu, 11 Nov 2021 15:03:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233819AbhKKPGD (ORCPT ); Thu, 11 Nov 2021 10:06:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232823AbhKKPGD (ORCPT ); Thu, 11 Nov 2021 10:06:03 -0500 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D69B8C061766 for ; Thu, 11 Nov 2021 07:03:13 -0800 (PST) Received: by mail-ed1-x52e.google.com with SMTP id b15so25116871edd.7 for ; Thu, 11 Nov 2021 07:03:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zoc7u92FnWKNwICURilcwlABFxlO+S2Ta7dqrANVtGo=; b=JXdByqakhYfi/toxdv5PptOIQicU7x3+7ymw83zVJrteNYpxd1v2KZ/4hoEeXqrFJU DHtIK6VndZLhzNH5G3+ib32c6ESbwBBUV3RS7nY3I9gl6mAWQsIBI/Cz+AM0pKaE14YR 24jQqDLbqrXv9hZG4uqpGPGfF9xs3kXCGJN3czldCO+FjY89qz6AnxEixuhTB5zVEUos 2RI3kJFrfxgcGp7O2LjAOkFxwWMgQjGGBqrE0pO7cJQfTHD4I2Lr2Sxey0m+FV1sMIKY 5d09nP+GOSNGSLNAJiHhc1tV6bLGquKjzDejN8a1JIwK9G2g1O2hlcDWI6IkO4lrY6dX SVxw== 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=zoc7u92FnWKNwICURilcwlABFxlO+S2Ta7dqrANVtGo=; b=mbboTAli07UvUxcY49xF2aMnUH80sJ7XDdzp5HqZH5jOvauOCflcWoE/WtOQpF3RMY ZL5gBwWfmz42b0DbqAi2N/8UgnJAVxKkJWOeEOE27j1yhdhbobE7gua6E0CvUiiOJa2i /MBB+uAEd1H53riPgMM2CPA/bIA1/d8peSqz9KvnHeqVRLQ+BagvyD33bwCm2aNPHFIf VRQPJWS6FfFLlt0tx9mhCT8OJN1OTQZ8Kq+XpH+9OyZK3Xpji2yobDIGv+SFtUYYvLai N4pCm9COdWD+hdY3UYTz97BA6BAIZGYgd4Uk1NUzCCdkV/jMjEQPOfdVImI8qic83yUl PGqA== X-Gm-Message-State: AOAM533qw73CXtdmbJMQqtVQ0aft1Z95s4gUQqXQsHrPsKvLRV0zSlFS Br91FCAiuEtw3la+e+lfQYE= X-Google-Smtp-Source: ABdhPJzU0DmOn4qCBxrYsMmEF6MHt8Ifd02dDpXlbwD6sgTznOku1xxgxveGBhU45u4xYZcOobnmnw== X-Received: by 2002:aa7:c956:: with SMTP id h22mr10707120edt.24.1636642992187; Thu, 11 Nov 2021 07:03:12 -0800 (PST) Received: from oberon.zico.biz.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id w3sm1737826edj.63.2021.11.11.07.03.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Nov 2021 07:03:11 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v5 4/6] trace-cmd library: Fix possible memory leak in read_ftrace_files() Date: Thu, 11 Nov 2021 17:03:03 +0200 Message-Id: <20211111150305.85943-5-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211111150305.85943-1-tz.stoyanov@gmail.com> References: <20211111150305.85943-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 f9dd0784..edf84993 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -618,28 +618,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 Thu Nov 11 15:06:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12614999 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4ACAAC433F5 for ; Thu, 11 Nov 2021 15:06:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2CCE861284 for ; Thu, 11 Nov 2021 15:06:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233075AbhKKPJL (ORCPT ); Thu, 11 Nov 2021 10:09:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231614AbhKKPJG (ORCPT ); Thu, 11 Nov 2021 10:09:06 -0500 Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1A09EC061766 for ; Thu, 11 Nov 2021 07:06:17 -0800 (PST) Received: by mail-ed1-x52c.google.com with SMTP id r12so25358567edt.6 for ; Thu, 11 Nov 2021 07:06:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=E9MjIrTo6m2QewfstLR4i2s1tGW2exN3IoUKp+UkBP8=; b=YjALq6BbMyVgQzs1OS2gULyPjXIlm25+nR4x8Q+HZ8I/ffpCzCmpGC/GReh0EFxipt kR9E94FWSDxmA+l+/mOe/iSoagHoJlNWvwJGjSqO/7GGCUr40pR/v9kkHmjhIwRlK3Fc /rtMuD1B0ZGITU2jQbXEWf1bg/DXkmt9h08NkxJrIpfVIrt4TRgO7BiCxVOoFWDs1yTp UJvOrXYAXfV+wbwQKo4bCLl0NPdgn13l6UDAcfEr6JM23sNQs8E4wfS+h8aRa/RtgF6K 5tfttUyJZLHumoscGlKjsGaUeZDhjzmJ8XfwbWzjby//vIsM2QBKyFPWBlNGHw7NVM8Z rp9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=E9MjIrTo6m2QewfstLR4i2s1tGW2exN3IoUKp+UkBP8=; b=6QNU+dc13vu+zb+ocpLjWnDyJLrYBFJRqEZRllUI2beUj5meORgfJ703EKH6raklFR +TlwE8Pg/GEA5WFF7QWTIGsLad35bvRRoZTKSUYR8e7Sc3YxVkAP14Ue/DTxu/y7+6eS oq8XfS1BS+plGw6EBe8s5jRQT9220+StBqBLXpJEFj1hkfj/ntzIp3r+BGg1gQOLbNwh T6f8Ya0OA6fdu3aOzaQl8i7PNz4Q/c2d6D0BfFpviTmGmJgdoxuTAD0E6v1R92Ew7D5o qfsh17ptg/wgXrSj1LXlcwNZOQbeDTDFl8GC5DV4fY0J9EvRztVKVXW1uSzmJpZp+VxB Lakw== X-Gm-Message-State: AOAM533LdUJdDa0WxQOHvWPrUN3CLLhEI41XqGqhfgc6Y3wXYbs5YPlb DBdCsTUbMvWpWzLQQV6qg0lbPYNxgm7ZTQ== X-Google-Smtp-Source: ABdhPJz7a+dAPtqnO5RQVO5WPsc+lqeYUoQ87dwVcI4vbcnhZYwqsHTvvO9ueLxXwuP54q4ZN5kVUQ== X-Received: by 2002:a17:906:6547:: with SMTP id u7mr10211825ejn.544.1636643175674; Thu, 11 Nov 2021 07:06:15 -0800 (PST) Received: from oberon.zico.biz.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id i10sm1595339ejw.48.2021.11.11.07.06.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Nov 2021 07:06:15 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v5 5/6] trace-cmd library: Set the correct file state when reading file with no kallsyms Date: Thu, 11 Nov 2021 17:06:14 +0200 Message-Id: <20211111150614.86172-1-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.33.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org The kallsyms section of the trace file can contain no data, this is valid use case. When such file is read and parsed by read_proc_kallsyms(), the file state should be set to TRACECMD_FILE_KALLSYMS in that case. Renamed a local variable from pevent to tep, to be consistent with the libtraceevent prefix. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-input.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index edf84993..f02d7e31 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -741,7 +741,7 @@ 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; @@ -750,8 +750,10 @@ static int read_proc_kallsyms(struct tracecmd_input *handle) if (read4(handle, &size) < 0) return -1; - if (!size) + if (!size) { + handle->file_state = TRACECMD_FILE_KALLSYMS; return 0; /* OK? */ + } buf = malloc(size+1); if (!buf) @@ -762,7 +764,7 @@ static int read_proc_kallsyms(struct tracecmd_input *handle) } buf[size] = 0; - tep_parse_kallsyms(pevent, buf); + tep_parse_kallsyms(tep, buf); free(buf); From patchwork Thu Nov 11 15:06:06 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: 12614997 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3C3C8C433EF for ; Thu, 11 Nov 2021 15:06:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 109B761038 for ; Thu, 11 Nov 2021 15:06:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232823AbhKKPI7 (ORCPT ); Thu, 11 Nov 2021 10:08:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231614AbhKKPI6 (ORCPT ); Thu, 11 Nov 2021 10:08:58 -0500 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5DC93C061766 for ; Thu, 11 Nov 2021 07:06:09 -0800 (PST) Received: by mail-ed1-x52e.google.com with SMTP id g14so25408054edz.2 for ; Thu, 11 Nov 2021 07:06:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=GFaftIMYHkExjqepWhbxWTn85mHgpiA3J55AMBFPiMw=; b=dHqzqYI/KCkWZLpKy+osAMlkuAqJvhyfwQjuYCWSrN/itpdlqbbGtgffDXxUcyDqzp jt27lBSwsBdbtHFBLes0kIy/OWR9xSJATb2eWbRgb/D8JIHuK5jXDhCQbJx8mdzhT7Wa 3YpFMBf7D74hhRRltZj3PZPSWv/1fEKc5gKhSUQbOh3Kr83nfhu6s84JSKIBmBGw2m5Q dyu31dW49UsJV++EMvtEgwC69lpi2yOvbQ9plPcsoFFNTL5k88Y7W/hL6+96vr7fUSlM mygAiqqJaq8FdpJFKapTfHE0hbdlt47eqtxpIDQzCD+30KveI3/hJl7YrCS9bPDAd2cT 9F6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=GFaftIMYHkExjqepWhbxWTn85mHgpiA3J55AMBFPiMw=; b=Nrf+U4z1RdRBbcqUlYNJPegC25TicvB4Q0DUirXHe2+gxuGc0eu60tIIbfP1yusKpj 9K0T2lYP7f1q+oA1ooOr96UVrW3q1hRgkxQTWs3omilaqN5fyNIsWGFJhk6iniQ7rGwA OMyML6eD/gSD3ni88bI6Yo1k5LfWCIKos+KQ+PRZEUgl0lsImN2sExFl3Ht01rI3k/C8 uE7xkzW8C3Ua2L9HjDkw2Jna6DFdqmKqu2YxngZxpOTdltfz7OawjvUWLReK4DSBN3F9 H2b75JIlB6C7EAlDEP1XtU5C/kclkLhCeb///AD/KCjVDFSDEE4wlb5JBH4aUQg3tWBA 4e3w== X-Gm-Message-State: AOAM533siHz5K48RS5AGwfXTojUqYs+oQ6w37yxcyrEm0Etxy4PipIqp NscQCNAu63DMzuVq6SXrJMq2gdGN9X33/A== X-Google-Smtp-Source: ABdhPJzvlgnsc0ZhBblGWFVXCPzyQAsDz7MZESzd3vXQrEhjF1TXGTpk+4NBr/V9vxOUx5BFzxlh/w== X-Received: by 2002:a05:6402:2815:: with SMTP id h21mr10825160ede.288.1636643167763; Thu, 11 Nov 2021 07:06:07 -0800 (PST) Received: from oberon.zico.biz.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id g1sm1465136eje.105.2021.11.11.07.06.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Nov 2021 07:06:07 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v5 6/6] trace-cmd library: Set the correct file state when reading file with no ftrace printk data Date: Thu, 11 Nov 2021 17:06:06 +0200 Message-Id: <20211111150606.86143-1-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.33.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org The ftrace printk section of the trace file can contain no data, this is valid use case. When such file is read and parsed by read_ftrace_printk, the file state should be set to TRACECMD_FILE_PRINTK in that case. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-input.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index f02d7e31..31e5e674 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -783,8 +783,10 @@ static int read_ftrace_printk(struct tracecmd_input *handle) if (read4(handle, &size) < 0) return -1; - if (!size) + if (!size) { + handle->file_state = TRACECMD_FILE_PRINTK; return 0; /* OK? */ + } buf = malloc(size + 1); if (!buf)