Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp4280694ybc; Tue, 26 Nov 2019 06:43:18 -0800 (PST) X-Google-Smtp-Source: APXvYqw/rzOY/gsiX0cIMFkFpwp2p2A0dC6bozdBtv+Plfo/zFQF2titEgP9AbXLHYoQT+JK/0pQ X-Received: by 2002:a50:9316:: with SMTP id m22mr25822899eda.39.1574779398745; Tue, 26 Nov 2019 06:43:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574779398; cv=none; d=google.com; s=arc-20160816; b=XYRKdGo5OWLXDGhJNFLtNRpDiMCYKJNFkHD+0UFGq735CelyF/saOdW2ABUw2PFhe/ GufoKcWW7g3iMNho9BV/8dPvs1vXzofh0BNBNuJ+5XfXIf8LaIPqpXWzpeSbdP2Kads/ b1HxePgiA0vG0WX9IcrINav6sytY3SrdI0x9D3v84c4iggiR1PXoz8E663hTzciW4GSG BYcALjyQ8NF4p4kyWpPz2/3D/u/nkKMjtU29S/lRuv2NB12eVo2MxUiOQMwBJVdyFjbe LjiKQ5x2lqI51ub+Lr6WbUSmlN+7Jpj5O93JaYIqc4mv5noXeQ1Jn/bLYml7Z+xMO4d2 pOPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Zrnv0oDKKER09MnS4EvMICed1va+rXp+dO15OJjiL2A=; b=y86DsW2ALKgrsYKhzBq1/J1GXfJDPXHtBaEvhMZxkayTL4MggYnBPiPeCloft9Bz51 Qi/ZoMSOIqMR7H+q83JcRyFhT2kaGnU63sX8+xRXcGN8KA7VOZnOOYV8OPJrtPcUxjA6 /cfZycJdwAFVN40Wn6BlFvPu70XZTngRy0KVpnMle46sxarjWCSKJD1Q5XiJWDSBocTQ y18A98VNW82THW8TMNW8yTzPsxhdBIAKbDYthr1QUxA7SkljdyANSkz8r/ViR1wJ97aX hE6VuyfjwMxjOHfMDCqe8wxAvbj8YnC/VUJfCHVMguY4mVD0Ni47f5oLnYMmCZ4vnzqS m8cg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=UZtkWe0V; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w18si6835823eji.346.2019.11.26.06.42.54; Tue, 26 Nov 2019 06:43:18 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=UZtkWe0V; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728318AbfKZOhu (ORCPT + 99 others); Tue, 26 Nov 2019 09:37:50 -0500 Received: from mail-pg1-f195.google.com ([209.85.215.195]:46318 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726101AbfKZOht (ORCPT ); Tue, 26 Nov 2019 09:37:49 -0500 Received: by mail-pg1-f195.google.com with SMTP id k1so731390pga.13 for ; Tue, 26 Nov 2019 06:37:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Zrnv0oDKKER09MnS4EvMICed1va+rXp+dO15OJjiL2A=; b=UZtkWe0VZ6TrDNroTbqABVRZEifpQd++CjqUJ6wyHGtUy2udOHPyLTC3QXB0B1YPqY vDU76/jBXovNgUR+ybt6ktqov7IpSrT8q9X5VmY/M84wWf+X2b36GhFXMdVc3eKpHSc3 0WaJUWW1Q59UtzZGUx5EHCZaCvgy9lhW/SGdifAzaxpyjuc4DaZdA8z6yCpO3F+FsfZ7 T/NbuxPEwOeUpY1zoFvYp+t/KTaKRdU4rGAeWJq2PsIYxlJRxLEGWkCGqP0bvOVLz1cb Ge6e7s0+jW55l1DNuGqsoee75w2M4tL58b8KV46lIQ22B6ZPdaQqTNRTu58qBaU9wtfR HO3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Zrnv0oDKKER09MnS4EvMICed1va+rXp+dO15OJjiL2A=; b=fD679l86ZZtwfw9pMHWrwTrrqGtW2P3vs+19TNFYhFp7YsGW7N65X7vDfk3c6vTcq5 v6O4Y5RawO03y4o26X9nOGlrebwBW3BismjL5WxjfXhTPwZW2OqywO5PkPHPd50nNqne e70ipF3RqaJ/n8+wc/3J/A5zj7vJLOmSZ56Xbyg/nIA6BhzvFlwPfi28QNDUv3FE9yNm 9MXdwb3Qcr8Bq0KDa/cdSnOcK/C6mz+djCf27km/dysixJjF4RMRMGbYCVGP1UkWqzJE R91LD2XorJXxkNXMeq1/zZXKVPHQe76V7GCju3q45WIyJa5IlQSM2kelqZMM1bUpnBUR bqlg== X-Gm-Message-State: APjAAAW94P226mkXxKL7F2LzRwR1EfSImMuSsDhBlgRWvgivFITLxxP2 Bsd95mMMEDgjTpHR5mCSdBM= X-Received: by 2002:a63:a34b:: with SMTP id v11mr17752819pgn.229.1574779068640; Tue, 26 Nov 2019 06:37:48 -0800 (PST) Received: from localhost.localdomain ([139.180.133.10]) by smtp.gmail.com with ESMTPSA id b13sm13139368pgj.28.2019.11.26.06.37.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Nov 2019 06:37:48 -0800 (PST) From: Changbin Du To: Jiri Olsa , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo Cc: Alexander Shishkin , Namhyung Kim , linux-kernel@vger.kernel.org, Changbin Du Subject: [PATCH v7 2/2] perf: add support for logging debug messages to file Date: Tue, 26 Nov 2019 22:37:20 +0800 Message-Id: <20191126143720.10333-3-changbin.du@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191126143720.10333-1-changbin.du@gmail.com> References: <20191126143720.10333-1-changbin.du@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- v5: doc default log path. v4: fix another segfault. v3: fix a segfault issue. --- tools/perf/Documentation/perf.txt | 6 ++++- tools/perf/util/debug.c | 44 ++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/tools/perf/Documentation/perf.txt b/tools/perf/Documentation/perf.txt index dcf9d98065f2..52c6ba825552 100644 --- a/tools/perf/Documentation/perf.txt +++ b/tools/perf/Documentation/perf.txt @@ -16,7 +16,8 @@ OPTIONS Setup debug variable (see list below). The range of 'level' value is (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 @@ -26,6 +27,9 @@ OPTIONS in browser mode perf-event-open - Print perf_event_open() arguments and return value + file[=path] - write debug output to log file, default + 'perf.log' (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 929da46ece92..21bc889976bc 100644 --- a/tools/perf/util/debug.c +++ b/tools/perf/util/debug.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -27,7 +28,7 @@ int verbose; int debug_peo_args; 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) @@ -35,8 +36,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); } @@ -198,6 +201,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; @@ -219,10 +240,25 @@ 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; + set_log_output(stderr); else if (!strcmp(opt, "perf-event-open")) debug_peo_args = true; - else { + else if (!strcmp(opt, "file")) { + FILE *f; + + if (!vstr) + vstr = (char *)"perf.log"; + + 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"); + set_log_output(f); + } else { fprintf(stderr, "unkown debug option '%s'\n", opt); free(dstr); return -1; -- 2.20.1