Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp1298220ybg; Fri, 18 Oct 2019 15:26:23 -0700 (PDT) X-Google-Smtp-Source: APXvYqzJO9sEX7Rg6+oqwcZVwC/DqR+RHjO/fyo+d5OanITpSZXYAWbebIV1pjZTRQgL9oy0p4xd X-Received: by 2002:aa7:d807:: with SMTP id v7mr12150553edq.220.1571437583120; Fri, 18 Oct 2019 15:26:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571437583; cv=none; d=google.com; s=arc-20160816; b=LWhLMrZ/gU+fMLnjE8O1iKIlHm++QW7vZCXuQUJVlHGKUxvyNrRvu+eH64u4crxrkS ubRqgcKo7nFq/W/5uCyNqUnln3tkE3xDvTRj7kvvvNiDDuOUP4YKp3KmpqWSeY/K05CA /t+qpRhgs/W28b0bdGeWYGWL3iYuAZyFo1rjH3Ip4B/7FGv4Yodex49IPTD5bP2vPa4R JidQWc36nx1UUubnXLaBGGsFjzF0PqK516phV+3WG4+nFqZPMGD6YWJUMgYhR/hzDOkR WlOotnP3vttg9yxI3e6KpG4WMSWol05jHsa4jS5Lq4N7ZfawcaenA6+jqIfZ/m/UW9hp HB5w== 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=0/AgeDaGZGO42j9ebgYZTy9x0zQEtmqzx5blEqfMmss=; b=NGZh7W9rK6PVlbRgSeGBwJb/KMZSNcGwgqDBr7INTJb/jvP/6z39YldKIEiQtemRV/ uJOcDTtCbz1/sKS/reQlL3FFZTOKA+knx3LWUbwW4bb/LCsLI8Lnj9d+03clYxB1VDd8 EBKvoR7B7P1ZUsRK6eB2b3wSp+Kmap0jBhJ3kCeyHiM/5CVltK6jYZbvHoYxWA4J1Fxj 60JlTwjRyilcqRvl1uukyrgPmCV+sraINII82hRYfK4PLVp9N+OI7ulwHTejxcMUYXBz +wm4w9OrFlmS2z1N2o54kNiMOj9O5HknzoI7jm0WMUdZdkPni1YLr8bvaU5w/NjvU6rS GmzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=YQECsyjC; 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 z54si5030904edd.337.2019.10.18.15.26.00; Fri, 18 Oct 2019 15:26:23 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=YQECsyjC; 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 S2404462AbfJRA21 (ORCPT + 99 others); Thu, 17 Oct 2019 20:28:27 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:37677 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728495AbfJRA20 (ORCPT ); Thu, 17 Oct 2019 20:28:26 -0400 Received: by mail-wr1-f67.google.com with SMTP id p14so4293298wro.4 for ; Thu, 17 Oct 2019 17:28:25 -0700 (PDT) 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=0/AgeDaGZGO42j9ebgYZTy9x0zQEtmqzx5blEqfMmss=; b=YQECsyjC+nZoz0FH5ycVzM1x1VsIw6Xjvlh+ckNwEI22KmLb2sCThylJRTklDdmoWL +zOixiGEbNb4ZBWygHLJgPtMgni+IIF9PJSR6t/OKDnW7SJpQGYaE1xp6m8nK2yaifx4 Kio2psPQFS3eYvCRqC0hjTXXTjfUmqYBRWjV/MoTBVFTPVuDvNAVhDU4nu4yCwaf8xxP rAZqkeeYJduhFa+sYWRO/47kJ6zHxhXDTRv7QL3RnHuvqDxQqesQ+dFfpme9Zr/JcR5y 02jCJpw3C0ZD+sIXuvBXffhGIwv/ReUbnxIfyIpao5yG/GJHMCA5AzyzXwLt71GrvWQi OFWQ== 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=0/AgeDaGZGO42j9ebgYZTy9x0zQEtmqzx5blEqfMmss=; b=QSmi8sgqSTU06e/ssPsNpayJX/kc2DgJoeCWjlrg2Sk8whjl3lfAUDrePBrNwGm60m 8cW8k1ahPPQAouaQ0ABvVJbi6KL90FdaQ5aGAnvVQ3R7vUH/lbTtDWmD3a2i7eu2pVoo OCleaBsr+bAfQi4+mi1MPAq1Ih8MfxF8d1IvSuCg9qBIRK96keUxRkM9bBlIRt12A2PM MyJa5JG/BmVhAxG3nrbqiT2wJ+tPZz5oDjrM59guZmjcEcTQ9Cnr1DHLjjaWPUpPpy9C NMoSsKFgKX/IO8GvsIV7p9z5l7qENlWREAdOYldv6VoX/MXHAWqXNIAcZL94t6TfMxzD yAbQ== X-Gm-Message-State: APjAAAXUR7iZnZnDgxS/Gz42wrJzYEFTTVmwnPjzTxpbmhezjQ5d3B93 JdP6s9EIVYZeKmv8CCF8JPE= X-Received: by 2002:adf:f24c:: with SMTP id b12mr5647700wrp.82.1571358504903; Thu, 17 Oct 2019 17:28:24 -0700 (PDT) Received: from localhost.localdomain ([104.238.174.53]) by smtp.gmail.com with ESMTPSA id p10sm4437649wrx.2.2019.10.17.17.28.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2019 17:28:23 -0700 (PDT) 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 v5 2/2] perf: add support for logging debug messages to file Date: Fri, 18 Oct 2019 08:27:57 +0800 Message-Id: <20191018002757.4112-3-changbin.du@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191018002757.4112-1-changbin.du@gmail.com> References: <20191018002757.4112-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 | 16 ++++++----- tools/perf/util/debug.c | 44 ++++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/tools/perf/Documentation/perf.txt b/tools/perf/Documentation/perf.txt index c05a94b2488e..48376be3c97a 100644 --- a/tools/perf/Documentation/perf.txt +++ b/tools/perf/Documentation/perf.txt @@ -16,14 +16,18 @@ 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, 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 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; - else { + set_log_output(stderr); + 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