Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp4737224ybp; Mon, 14 Oct 2019 09:14:13 -0700 (PDT) X-Google-Smtp-Source: APXvYqx/VqCAyCjRVp4pSKdtmghUcC92C9gGh1uAooAqF/4PomnsuVX3gdgsMdjpCXXWqvsiIR36 X-Received: by 2002:a17:906:80cd:: with SMTP id a13mr29848444ejx.12.1571069653615; Mon, 14 Oct 2019 09:14:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571069653; cv=none; d=google.com; s=arc-20160816; b=tj+apEKtwIl/m9jYionQgfL2tKkz06SM+Jtm6GspHg9k1Wq7Lk6NmqX9zTPc+0wn/z Li1mTC2wTIv7dB6pP4c9Fx22zIIRL2Fth0JO0ICCTcr+u6o9IwPI0XM544Nw1cVEktxO uJVw0tDuG4DYu3oK88LQfR8BGlSP7e7YEKxWcBxSWjGgrQ47ewr2/M29Tzw5r2eUVxg3 /SRqgog5fNlDg+HaGKJ2fRq0Q9N7LsHvtuka3UQQFkAd4/8QO5muftg9mKg3YPtt72Wz GhqPV9a409Cd05DdS42axCTKj0zOETpEZqQpQjbeM8iBJPeuFBZPsRcH/L6tmMrPbZli 9N6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=j225Wf3w8ZBBWWr5pswS6pGIl7TS5XzaXKU+TrJVrDI=; b=dUCeH74IqFshrmnlfPkET6uCrvhYahvu3WdBQ1RKypih5XJF9qcs2BoQNdfl7IdB4Y dV0j60NFSKozqPvZY+ZxMxjBDEzbC0e4WRjb/Y9pixJ5qRTf8T0pBSc4HqHuUYGYaGsX yxPQfhAL4tcJ/5/0eLTbUafaPHByt7C9YucFH6x/QTRAeIKnZW1cmC/Seyxyg/q9VSvb TqWJ3r9LdP1udGFU7TQyQDZltn6zwPOmvGULGKPAXwsXO2o6VJqFmoTkGVcvQtsvTwkZ +GJOvRwbQ2E5DUsQl0Cy09eaT/AZfOnuFqm5C6xVnZyloFt857cMckiNK6EpB0F3cAX4 0R7g== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b26si11817529edy.108.2019.10.14.09.13.17; Mon, 14 Oct 2019 09:14:13 -0700 (PDT) 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387504AbfJNP35 (ORCPT + 99 others); Mon, 14 Oct 2019 11:29:57 -0400 Received: from mx1.redhat.com ([209.132.183.28]:39684 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729905AbfJNP34 (ORCPT ); Mon, 14 Oct 2019 11:29:56 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 22CA9308FB9A; Mon, 14 Oct 2019 15:29:56 +0000 (UTC) Received: from krava (unknown [10.40.205.218]) by smtp.corp.redhat.com (Postfix) with SMTP id 39238196AE; Mon, 14 Oct 2019 15:29:54 +0000 (UTC) Date: Mon, 14 Oct 2019 17:29:53 +0200 From: Jiri Olsa To: Changbin Du Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Alexander Shishkin , Namhyung Kim , linux-kernel@vger.kernel.org Subject: Re: [PATCH v4 2/2] perf: add support for logging debug messages to file Message-ID: <20191014152953.GD9700@krava> References: <20191008123554.6796-1-changbin.du@gmail.com> <20191008123554.6796-3-changbin.du@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20191008123554.6796-3-changbin.du@gmail.com> User-Agent: Mutt/1.12.1 (2019-06-15) X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.43]); Mon, 14 Oct 2019 15:29:56 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 08, 2019 at 08:35:54PM +0800, Changbin Du wrote: > When in TUI mode, it is impossible to show all the debug messages to > console. This make it hard to debug perf issues using debug messages. > This patch adds support for logging debug messages to file to resolve > this problem. > > The usage is: > perf -debug verbose=2,file=~/perf.log COMMAND > > Signed-off-by: Changbin Du > > -- > v4: fix another segfault. > v3: fix a segfault issue. hi, no crash this time ;-) some questions below > --- > tools/perf/Documentation/perf.txt | 15 ++++++----- > tools/perf/util/debug.c | 44 ++++++++++++++++++++++++++++--- > 2 files changed, 49 insertions(+), 10 deletions(-) > > diff --git a/tools/perf/Documentation/perf.txt b/tools/perf/Documentation/perf.txt > index c05a94b2488e..a6b19661e5c3 100644 > --- a/tools/perf/Documentation/perf.txt > +++ b/tools/perf/Documentation/perf.txt > @@ -16,14 +16,17 @@ OPTIONS > Setup debug variable (see list below) in value > range (0, 10). Use like: > --debug verbose # sets verbose = 1 > - --debug verbose=2 # sets verbose = 2 > + --debug verbose=2,file=~/perf.log > + # sets verbose = 2 and save log to file > > List of debug variables allowed to set: > - verbose=level - general debug messages > - ordered-events=level - ordered events object debug messages > - data-convert=level - data convert command debug messages > - stderr - write debug output (option -v) to stderr > - in browser mode > + verbose=level - general debug messages > + ordered-events=level - ordered events object debug messages > + data-convert=level - data convert command debug messages > + stderr - write debug output (option -v) to stderr > + in browser mode > + file=path - write debug output to log file (stderr and > + file options are exclusive) > > --buildid-dir:: > Setup buildid cache directory. It has higher priority than > diff --git a/tools/perf/util/debug.c b/tools/perf/util/debug.c > index df82ad9cd16d..5cc2479d63ea 100644 > --- a/tools/perf/util/debug.c > +++ b/tools/perf/util/debug.c > @@ -6,6 +6,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -26,7 +27,7 @@ > int verbose; > bool dump_trace = false, quiet = false; > int debug_ordered_events; > -static bool redirect_to_stderr; > +static FILE *log_file; > int debug_data_convert; > > int veprintf(int level, int var, const char *fmt, va_list args) > @@ -34,8 +35,10 @@ int veprintf(int level, int var, const char *fmt, va_list args) > int ret = 0; > > if (var >= level) { > - if (use_browser >= 1 && !redirect_to_stderr) > + if (use_browser >= 1 && !log_file) > ui_helpline__vshow(fmt, args); > + else if (log_file) > + ret = vfprintf(log_file, fmt, args); > else > ret = vfprintf(stderr, fmt, args); > } > @@ -197,6 +200,24 @@ static int str2loglevel(const char *vstr) > return v; > } > > +static void flush_log(void) > +{ > + if (log_file) > + fflush(log_file); > +} > + > +static void set_log_output(FILE *f) > +{ > + if (f == log_file) > + return; > + > + if (log_file && log_file != stderr) > + fclose(log_file); > + > + log_file = f; > + atexit(flush_log); > +} > + > int perf_debug_option(const char *str) > { > char *sep, *vstr; > @@ -218,8 +239,23 @@ int perf_debug_option(const char *str) > else if (!strcmp(opt, "data-convert")) > debug_data_convert = str2loglevel(vstr); > else if (!strcmp(opt, "stderr")) > - redirect_to_stderr = true; do you ned to fflush stderr? I thought it's unbuffered > - else { > + set_log_output(stderr); > + else if (!strcmp(opt, "file")) { > + FILE *f; > + > + if (!vstr) > + vstr = (char *)"perf.log"; you did not mention in doc that perf.log is default > + > + f = fopen(vstr, "a"); > + if (!f) { > + pr_err("Can not create log file: %s\n", > + strerror(errno)); > + free(dstr); > + return -1; > + } > + fprintf(f, "\n===========perf log===========\n"); is this necessary? jirka > + set_log_output(f); > + } else { > fprintf(stderr, "unkown debug option '%s'\n", opt); > free(dstr); > return -1; > -- > 2.20.1 >