Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp5601843ybp; Tue, 8 Oct 2019 05:39:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqy2g9BGYVttLtLPYUrumSRm015fYRRU0sCu99xiFiYVNo1K5PVF45UC2GtK4bryC/mJaRKj X-Received: by 2002:a17:906:2542:: with SMTP id j2mr28427055ejb.278.1570538372121; Tue, 08 Oct 2019 05:39:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570538372; cv=none; d=google.com; s=arc-20160816; b=I+XwmUhlE9JkHgp9IpI0ljoNKboh0r7bxv5UFNbScpbFpdkyAsp29H0trIiwKdk5qh wGUDCbAecLZ0mfwcHE5YatiXInIjI1eiw82ajF1HJM+zAmO8/N14OASVpPagcYjNSdUo ielPDsOBnqL36u+DDT3oEG2ldAgTHfCgs5UyJij3wb8b0rZ6K9+EF2iJwNwnvMm/cMA+ UpvIiRfeGup1xfuW3VbP4UWQdwpJJAv5hYtwxmm7Atz91sKa+dR6VoV3cA6J8CiHyFJZ cn6QRNqym+xbb02t3j7I5BFC1MlDDGWdspvlvmldXQJ5VArzhoIPHvil0KZ3CZ89tI4I Zecg== 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=5V4/YTrCDA2Y4CjapiR7kwxoAP4iI0pi4NlrpV9i7/0=; b=bEJH9mTYMoLMH77sYmkVXfvrAmtoMfqK3ikmmLmcMRyS/x9BAaQUHqlqoSTZm9xkE6 alrYpeTH/zKYFpaCXC9xw9pLgI3rKVzemuMyqUCIa+Qp0s573hPSRBQbuK2hUKZca9mv Ihdzp+/fak7OJs8seFvykAntqQrTv8FI9aGNZWmE/qsFFRxxfmtyND1dKJ8mY5Pk4Dz9 iX0ZNiRymKmiCGHQsuN5vEV2UBuJZ6VMyqZoGf8WWk3EE9CCj1iAscJ5KMWz/wLQ/BAm /PIk0E2RSGW46yDP7Yu6tLqsok0h8CbyvLuoqNz9cAq0F88g2wRc0L7ga2yCV8upbfJ+ +r1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=pp9EPYPC; 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 r19si791307eji.376.2019.10.08.05.39.09; Tue, 08 Oct 2019 05:39:32 -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=pp9EPYPC; 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 S1730980AbfJHMgW (ORCPT + 99 others); Tue, 8 Oct 2019 08:36:22 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:34928 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730249AbfJHMgV (ORCPT ); Tue, 8 Oct 2019 08:36:21 -0400 Received: by mail-wm1-f66.google.com with SMTP id y21so2964627wmi.0 for ; Tue, 08 Oct 2019 05:36:20 -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=5V4/YTrCDA2Y4CjapiR7kwxoAP4iI0pi4NlrpV9i7/0=; b=pp9EPYPCdjMCuyJsvWvvT6MesZUAFpzob9Gpw4+F3i4leWX87MqaB1V3TwNfO6jHYn /HQ9HeLAo2Nvm0iy/1vEUWtDN7zePVCxBoOXDy9q7mYtNv/5EhMkYuro+GYZhA46EHat bg6yGpRbClI5LIQocIG5Opfm4T65xHsDSnZgMkXDHAX0ecYoqbqbrePiW99vpvaBaHLK 0rN7RFcx5CwkrZU+F76fZggf4UgKUIuC7RSBcWxe+Gpfw9jQSsrzgqvlgra69dUs5+1X 9ER8NQkGzgHA19qsmcE6Tk/4mxsQ8V3PVpRJkvAHhb/wmQQzDhO/04jG2WFdXI1+5KLw aWFQ== 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=5V4/YTrCDA2Y4CjapiR7kwxoAP4iI0pi4NlrpV9i7/0=; b=NOJmtGIuWvVrQc2VhpowxC3+De5QhkQ3s53GSEJAezo0NEanFN6FnovgeLv3V0VVyn QQlEAcfuzn5GekjrVrhL5f3ZRvTDbYIdzSIflgQzFugHi6qz4UdZbh6QLeNAclbqZ4Th 4xvzChNeayUvInmeCeXc+rWJrGgAPOcFomvjjYUBfpkmjDfCJnR33c6fkotHtaubNjaB V0S1/Uap4fXi84tHmzoNzCBu8RQIOp3fuELw8JqlbhGUKw13i++EqfxGv7nCRcqhFCQ3 ckWy3jmnKjX3WJ3/EiTYxl2XhkB3G9/vMxcXpR9S0TlZTKZT3pQYE7koapiaTxq/M1pF V63w== X-Gm-Message-State: APjAAAUZgXNSibPzMNxJHKk5Y4ySfdK+jIyPpKOlRGXPZMelSPMAl9eI VuLlfRWUoOXLuInfKVAnPwoTCTRUXNQ= X-Received: by 2002:a1c:4456:: with SMTP id r83mr3655361wma.44.1570538179357; Tue, 08 Oct 2019 05:36:19 -0700 (PDT) Received: from localhost.localdomain ([104.238.174.53]) by smtp.gmail.com with ESMTPSA id d4sm23100575wrq.22.2019.10.08.05.36.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2019 05:36:18 -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 v4 2/2] perf: add support for logging debug messages to file Date: Tue, 8 Oct 2019 20:35:54 +0800 Message-Id: <20191008123554.6796-3-changbin.du@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191008123554.6796-1-changbin.du@gmail.com> References: <20191008123554.6796-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 -- v4: fix another segfault. v3: fix a segfault issue. --- 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; - 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