Received: by 10.223.185.116 with SMTP id b49csp3264227wrg; Sun, 18 Feb 2018 18:43:05 -0800 (PST) X-Google-Smtp-Source: AH8x224M5clNSzZUiujI7KRpge0HAWZ7g8ZyW7DasUde1eGiH3W6kLDrEfbjnAE7MCKX2hXG2u6q X-Received: by 2002:a17:902:7586:: with SMTP id j6-v6mr11630031pll.301.1519008185101; Sun, 18 Feb 2018 18:43:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519008185; cv=none; d=google.com; s=arc-20160816; b=asG/u/hUGtuopnL5shBEQLRPFkWYfUfMzdJfaAvPGPvMthmUnoq3lv72X4VyFUhwQ7 2/seHge8kon1PKW/BXE1VTQ+JA5UtSRNlT/wNpP3BafjCzEpKKIG8uf9LOvNSBQTUiJW HSsxCcLQUZQsaGhIcAx28+2v2wnMdd+cpp8XfbPSd8Ai0QK0VPbW9K5JS3LPutTThJeR UO0UY8crExWYHVnmszN7/DzsP2RtHNPeAR3rd4AcmnMl1TeSJBSBzrvcsYIjlt5Qosb6 Oxou2OM46vcchoNdjr5ofMcepDVcs/A1dkvuywb4d0lqoI85nCSAa6BJFVh4wk2F6d7F tgdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=V7s79qc6K0rOSkj9VVhbkLXfMJjzXwIeJF9PCXTO0h4=; b=UTHM0GFOOP0110dCN0gyFwWKy6O55RCW/uPjxcI34H5/L+mgY73PFxgV9jEniNjKC0 5Lffzr+HePD+Ksiw18UroOeh1MfH9DGv92Y8keV+Jt/EU7d32sj5phZEc1tJ0bPkRj4G yBLSYtahQqUxUblR5XAqXQoCzNMZDIS7fGVqTcxJGKuewcvwzhMlRtgKInJGsvhEYaL0 ByocBuMEcZM6GOTslKPxgTk9TcxSnhRQIiXmAhsJ3VROpOOLaGhTJTBxD4buVAdc1vTP P1piblSwu4ftVOxrxC+OpIw1Q2axhXphY6BJC15NG/RSfA8Z67oywEsn5IqRXohzlc/s ps6w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q22si5961974pfk.225.2018.02.18.18.42.51; Sun, 18 Feb 2018 18:43:05 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752253AbeBSCmP (ORCPT + 99 others); Sun, 18 Feb 2018 21:42:15 -0500 Received: from mga01.intel.com ([192.55.52.88]:44934 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752075AbeBSCmO (ORCPT ); Sun, 18 Feb 2018 21:42:14 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Feb 2018 18:42:13 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,533,1511856000"; d="scan'208";a="18657239" Received: from gvt-dell.bj.intel.com (HELO gvt-dell-host.bj.intel.com) ([10.238.154.59]) by fmsmga007.fm.intel.com with ESMTP; 18 Feb 2018 18:42:12 -0800 From: changbin.du@intel.com To: jolsa@redhat.com Cc: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Changbin Du Subject: [PATCH v4] perf ftrace: Append an EOL when write tracing files Date: Mon, 19 Feb 2018 10:33:29 +0800 Message-Id: <1519007609-14551-1-git-send-email-changbin.du@intel.com> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Changbin Du Before this change, the '--graph-funcs', '--nograph-funcs' and '--trace-funcs' options didn't work as expected when the doesn't exist. Because the kernel side hid possible errors. $ sudo ./perf ftrace -a --graph-depth 1 --graph-funcs abcdefg 0) 0.140 us | rcu_all_qs(); 3) 0.304 us | mutex_unlock(); 0) 0.153 us | find_vma(); 3) 0.088 us | __fsnotify_parent(); 0) 6.145 us | handle_mm_fault(); 3) 0.089 us | fsnotify(); 3) 0.161 us | __sb_end_write(); 3) 0.710 us | SyS_close(); 3) 7.848 us | exit_to_usermode_loop(); On above example, I specified function filter 'abcdefg' but all functions are enabled. The expected error is hidden. The original fix is to make the kernel support '\0' as end of string: https://lkml.org/lkml/2018/1/16/116 But above fix cannot be compatible with old kernels. Then Namhyung Kim suggest adding a space after function name. This patch will append an '\n' when write tracing file. After this fix, the perf will report correct error state. Also let it print an error if reset_tracing_files() fails. Cc: Namhyung Kim Signed-off-by: Changbin Du --- v4: check strdup() return. (Kim) v3: Took Kim's suggestion that add a space after function name. v2: Rebase. --- tools/perf/builtin-ftrace.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c index 25a42ac..f42f228 100644 --- a/tools/perf/builtin-ftrace.c +++ b/tools/perf/builtin-ftrace.c @@ -72,6 +72,7 @@ static int __write_tracing_file(const char *name, const char *val, bool append) ssize_t size = strlen(val); int flags = O_WRONLY; char errbuf[512]; + char *val_copy; file = get_tracing_file(name); if (!file) { @@ -91,12 +92,23 @@ static int __write_tracing_file(const char *name, const char *val, bool append) goto out; } - if (write(fd, val, size) == size) + /* + * Copy the original value and append a '\n'. Without this, + * the kernel can hide possible errors. + */ + val_copy = strdup(val); + if (!val_copy) + goto out_close; + val_copy[size] = '\n'; + + if (write(fd, val_copy, size + 1) == size + 1) ret = 0; else pr_debug("write '%s' to tracing/%s failed: %s\n", val, name, str_error_r(errno, errbuf, sizeof(errbuf))); + free(val_copy); +out_close: close(fd); out: put_tracing_file(file); @@ -280,8 +292,10 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv) signal(SIGCHLD, sig_handler); signal(SIGPIPE, sig_handler); - if (reset_tracing_files(ftrace) < 0) + if (reset_tracing_files(ftrace) < 0) { + pr_err("failed to reset ftrace\n"); goto out; + } /* reset ftrace buffer */ if (write_tracing_file("trace", "0") < 0) -- 2.7.4