From patchwork Tue Sep 3 13:34:42 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 11127899 X-Patchwork-Delegate: rostedt@goodmis.org Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B4D161399 for ; Tue, 3 Sep 2019 13:34:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8BF5C23697 for ; Tue, 3 Sep 2019 13:34:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="r3IFC3lN" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729090AbfICNeu (ORCPT ); Tue, 3 Sep 2019 09:34:50 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:36889 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729079AbfICNeu (ORCPT ); Tue, 3 Sep 2019 09:34:50 -0400 Received: by mail-wr1-f67.google.com with SMTP id z11so17525334wrt.4 for ; Tue, 03 Sep 2019 06:34:48 -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=zmnqcMgO4qWN68jFns4KSCtJ6lJMvztIzKeN5/wCFiw=; b=r3IFC3lNLPBUYj4F1odLyjPGZsNcUykmBlDO1pfW4UZK3a3v9FccuOKjTU1JKLCr8m jsyR18Pf31ldmAX5TDmoRuuZm8fpeiQEOqCuNY6UL9k/yoqUTYL8Qc4+BLPDGS0eIoqf u3e7TNYWX31eKnTcleMSAcy98awHtM5BRFj5hEaGwmbrXb3QLEKN7hOruCYe6AjCQ7Fd hT01b0/HpST4f5tZPk9XQ8foAdSQ/4imX11tQHEsBzgETGCkt9hPRtgqS53FHa1i6UDr hF0BMVAQgI4lUKWbbHgUKKVgfUKFdkoY5bmLUzwOOYjkeIfV6hPLSwpkGKYT/05yt/FE FdXQ== 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=zmnqcMgO4qWN68jFns4KSCtJ6lJMvztIzKeN5/wCFiw=; b=kfHDdLsWJlI4C5wn6jxNbswik8CgQfeq+p4iYimU96SDU+fNVwC12Rd/gbPuIhQAv2 7CoLb9DXrKW4Jz5eZnZaRB3yUBOqxaVDOAT8kspez6xzT+xGWcVawcc5VAFQqJf/Bh9o JDHq/0M3wvhPLdcObh3vvH6DsOywRUcQCJXXs/X/x0Cicq0fFD09GwsrivP3CG+RlRrZ 6xhKbrNSfGpoDyNXEBUmNNfmU6VQOYHx0AKBRw9PEJ9/tnBE0+Up//j7kxaebx1F5eLX XauQTNodUI1sdzQPxI+DlZ0Bm19spMxbcjO3hfMchCVCfucuocdzYnoHH/GLV4QzDl5I 3scg== X-Gm-Message-State: APjAAAWXvnxo8oFPh1KNVX1AiDnjsG3rHjxvue97vi5wlqJwmgc4LyZL dPjeP6Nflwp6+PdCsZ10PvJ6ZInJ X-Google-Smtp-Source: APXvYqyq4TuKmhgWZbsr7wIx6fufZXvA7V4ZgPgqC12/mBSJz9E+dIyyZJfzdXuWMCFzWG4KuyuDOw== X-Received: by 2002:a5d:51c7:: with SMTP id n7mr41453937wrv.73.1567517687892; Tue, 03 Sep 2019 06:34:47 -0700 (PDT) Received: from oberon.eng.vmware.com ([146.247.46.5]) by smtp.gmail.com with ESMTPSA id r16sm33509833wrc.81.2019.09.03.06.34.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2019 06:34:47 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 1/4] trace-cmd: Move trace-cmd global variable "quiet" to libtracecmd Date: Tue, 3 Sep 2019 16:34:42 +0300 Message-Id: <20190903133445.30486-2-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190903133445.30486-1-tz.stoyanov@gmail.com> References: <20190903133445.30486-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Sender: linux-trace-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org A trace-cmd global variable "quiet" is used from libtracecmd and should be defined there. A new library APIs are implemented to access it: void tracecmd_set_quiet(int quiet); int tracecmd_get_quiet(void); Signed-off-by: Tzvetomir Stoyanov (VMware) --- include/trace-cmd/trace-cmd.h | 3 +++ lib/trace-cmd/include/trace-cmd-local.h | 2 -- lib/trace-cmd/trace-output.c | 28 +++++++++++++++++++++++-- tracecmd/include/trace-local.h | 2 -- tracecmd/trace-cmd.c | 2 -- tracecmd/trace-record.c | 15 +++++++++---- 6 files changed, 40 insertions(+), 12 deletions(-) diff --git a/include/trace-cmd/trace-cmd.h b/include/trace-cmd/trace-cmd.h index ede510c..f44f7a4 100644 --- a/include/trace-cmd/trace-cmd.h +++ b/include/trace-cmd/trace-cmd.h @@ -57,6 +57,9 @@ struct tracecmd_output; struct tracecmd_recorder; struct hook_list; +void tracecmd_set_quiet(struct tracecmd_output *handle, bool set_quiet); +bool tracecmd_get_quiet(struct tracecmd_output *handle); + static inline int tracecmd_host_bigendian(void) { unsigned char str[] = { 0x1, 0x2, 0x3, 0x4 }; diff --git a/lib/trace-cmd/include/trace-cmd-local.h b/lib/trace-cmd/include/trace-cmd-local.h index bad325f..09574db 100644 --- a/lib/trace-cmd/include/trace-cmd-local.h +++ b/lib/trace-cmd/include/trace-cmd-local.h @@ -18,8 +18,6 @@ #define STR(x) _STR(x) #define FILE_VERSION_STRING STR(FILE_VERSION) -extern int quiet; - static ssize_t __do_write(int fd, const void *data, size_t size) { ssize_t tot = 0; diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index 1f94346..41932ee 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -53,6 +53,7 @@ struct tracecmd_output { char *tracing_dir; int options_written; int nr_options; + bool quiet; struct list_head options; struct tracecmd_msg_handle *msg_handle; }; @@ -103,6 +104,29 @@ static unsigned long long convert_endian_8(struct tracecmd_output *handle, return tep_read_number(handle->pevent, &val, 8); } +/** + * tracecmd_set_quiet - Set if to print output to the screen + * @quiet: If non zero, print no output to the screen + * + */ +void tracecmd_set_quiet(struct tracecmd_output *handle, bool set_quiet) +{ + if (handle) + handle->quiet = set_quiet; +} + +/** + * tracecmd_get_quiet - Get if to print output to the screen + * Returns non zero, if no output to the screen should be printed + * + */ +bool tracecmd_get_quiet(struct tracecmd_output *handle) +{ + if (handle) + return handle->quiet; + return false; +} + void tracecmd_output_free(struct tracecmd_output *handle) { struct tracecmd_option *option; @@ -1157,7 +1181,7 @@ int tracecmd_write_cpu_data(struct tracecmd_output *handle, goto out_free; for (i = 0; i < cpus; i++) { - if (!quiet) + if (!tracecmd_get_quiet(handle)) fprintf(stderr, "CPU%d data recorded at offset=0x%llx\n", i, (unsigned long long) offsets[i]); offset = lseek64(handle->fd, offsets[i], SEEK_SET); @@ -1172,7 +1196,7 @@ int tracecmd_write_cpu_data(struct tracecmd_output *handle, check_size, sizes[i]); goto out_free; } - if (!quiet) + if (!tracecmd_get_quiet(handle)) fprintf(stderr, " %llu bytes in size\n", (unsigned long long)check_size); } diff --git a/tracecmd/include/trace-local.h b/tracecmd/include/trace-local.h index 7de5dd6..05760d8 100644 --- a/tracecmd/include/trace-local.h +++ b/tracecmd/include/trace-local.h @@ -12,8 +12,6 @@ #include "trace-cmd.h" #include "event-utils.h" -extern int quiet; - /* fix stupid glib guint64 typecasts and printf formats */ typedef unsigned long long u64; diff --git a/tracecmd/trace-cmd.c b/tracecmd/trace-cmd.c index 4add2e2..30691b6 100644 --- a/tracecmd/trace-cmd.c +++ b/tracecmd/trace-cmd.c @@ -16,8 +16,6 @@ int silence_warnings; int show_status; -int quiet; - void warning(const char *fmt, ...) { va_list ap; diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index 7467b47..61457c8 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -80,6 +80,8 @@ static char *host; static unsigned int *client_ports; static int sfd; +static bool quiet; + /* Max size to let a per cpu file get */ static int max_kb; @@ -3162,13 +3164,16 @@ setup_connection(struct buffer_instance *instance, struct common_record_context /* Now create the handle through this socket */ if (msg_handle->version == V3_PROTOCOL) { network_handle = tracecmd_create_init_fd_msg(msg_handle, listed_events); + tracecmd_set_quiet(network_handle, quiet); add_options(network_handle, ctx); tracecmd_write_cpus(network_handle, instance->cpu_count); tracecmd_write_options(network_handle); tracecmd_msg_finish_sending_data(msg_handle); - } else + } else { network_handle = tracecmd_create_init_fd_glob(msg_handle->fd, listed_events); + tracecmd_set_quiet(network_handle, quiet); + } instance->network_handle = network_handle; @@ -3444,9 +3449,10 @@ static void record_data(struct common_record_context *ctx) if (!local) return; - if (latency) + if (latency) { handle = tracecmd_create_file_latency(output_file, local_cpu_count); - else { + tracecmd_set_quiet(handle, quiet); + } else { if (!local_cpu_count) return; @@ -3478,6 +3484,7 @@ static void record_data(struct common_record_context *ctx) handle = tracecmd_create_init_file_glob(output_file, listed_events); if (!handle) die("Error creating output file"); + tracecmd_set_quiet(handle, quiet); add_options(handle, ctx); @@ -5155,7 +5162,7 @@ static void parse_record_options(int argc, break; case OPT_quiet: case 'q': - quiet = 1; + quiet = true; break; default: usage(argv); From patchwork Tue Sep 3 13:34:43 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 11127901 X-Patchwork-Delegate: rostedt@goodmis.org Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 61A7316B1 for ; Tue, 3 Sep 2019 13:34:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 38E8B2343A for ; Tue, 3 Sep 2019 13:34:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="qMCaQjba" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729109AbfICNew (ORCPT ); Tue, 3 Sep 2019 09:34:52 -0400 Received: from mail-wr1-f48.google.com ([209.85.221.48]:45422 "EHLO mail-wr1-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725782AbfICNev (ORCPT ); Tue, 3 Sep 2019 09:34:51 -0400 Received: by mail-wr1-f48.google.com with SMTP id q12so17484644wrj.12 for ; Tue, 03 Sep 2019 06:34:49 -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=VPwam2csJrwR6+4W/GwkgtNQvtWoArv5NAaZJm/Jv80=; b=qMCaQjbaC8dg5bIoP2fH+r0LUTb7rSAxOeUoAiY0L6fcwOMXQVhT41P8nTwKI5eRvg j0sISZToE/hFrIX/fMUsIN5vVLuKHG/PkHoyLeR8Chg6b2IzZ1zABko6ibjodyUzKtoU biTaSA0+kchs0J9/lMGBk2W+l/o01uyW5mXQEwro+RRT4FCsLy9BZRJTP41mhki8Y+mt uuaNU3xHa3NykpxUFR/amI9eLnkvJ6OYElxRcbQ7976MpAT+itgxjuOoAXLvMLn1ASUf vnegT/mDe3A/kMLd3tUX0JvS1tA6zwjtghLMekJT0APN8OuSJ3BTDnzjX0SNPjAKMg7w rhng== 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=VPwam2csJrwR6+4W/GwkgtNQvtWoArv5NAaZJm/Jv80=; b=Nghr6GGUzDzl3TJKMxn/YH+cnyM2WvaOYanmYDFYoP35iJ4JB7ha/CWBBRf9xsgKXI hSEFf1SRiZ4/2pUTs0IXp1mYOD+owPC/oOxXiUt2SJu1BEBl3r0H3k5r3vXvtWrKujYX nVB1Iz8FKu6Xgb5HOuBuYFmt1BeK+AsywUhF5nfX5LPTpXgluj3n5LejoUJkEunfSXL7 v546fB/5yl4wY85UttPFzofoCAtNfC9eChFre+YdJ967B7QBUEjA0lWGm/DxfMBcybGd iBNZkSKiduq/y2gQczsr5HLtyJFe5W344vNggFakRKFJVh0AF5CnroN/3edCtE0WS62z dNNA== X-Gm-Message-State: APjAAAVmW4t4KZHCkZogSQ1rMHGCqGk6KTV5ys85Kcbz9DdQBYNDGYlu dMApj/VUARBLBfOTTaFNH3I= X-Google-Smtp-Source: APXvYqxLmtDLO4s3xdSP1ouYkDuTYcXEget0h6RMYaFe9hEceam0E5NNtZUp5tqFcZAdnPaArSq+tg== X-Received: by 2002:a05:6000:1c3:: with SMTP id t3mr1617411wrx.76.1567517688915; Tue, 03 Sep 2019 06:34:48 -0700 (PDT) Received: from oberon.eng.vmware.com ([146.247.46.5]) by smtp.gmail.com with ESMTPSA id r16sm33509833wrc.81.2019.09.03.06.34.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2019 06:34:48 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 2/4] trace-cmd: Move plog() function to libtracecmd. Date: Tue, 3 Sep 2019 16:34:43 +0300 Message-Id: <20190903133445.30486-3-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190903133445.30486-1-tz.stoyanov@gmail.com> References: <20190903133445.30486-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Sender: linux-trace-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org plog() function writes logs into a log file. It is used in libtracecmd and its implementation should be there. The function is moved from trace-cmd into the library and renamed to tracecmd_plog(). Two additional APIs are implemented: int tracecmd_set_log_file(char *logfile); - use it to set the log file. void tracecmd_plog_error(const char *fmt, ...); - use it to log an error message into the file. The plog() function is used also from pdie() in trace-cmd. pdie() depends on trace-cmd context and cannot be moved to the library. It is reimplemented as macros, in order to utilize the new tracecmd_plog() library function. Signed-off-by: Tzvetomir Stoyanov (VMware) --- include/trace-cmd/trace-cmd.h | 4 ++ include/trace-cmd/trace-msg.h | 3 -- lib/trace-cmd/trace-msg.c | 8 ++-- lib/trace-cmd/trace-util.c | 70 +++++++++++++++++++++++++++ tracecmd/trace-listen.c | 89 +++++++---------------------------- tracecmd/trace-record.c | 2 +- 6 files changed, 96 insertions(+), 80 deletions(-) diff --git a/include/trace-cmd/trace-cmd.h b/include/trace-cmd/trace-cmd.h index f44f7a4..de3183d 100644 --- a/include/trace-cmd/trace-cmd.h +++ b/include/trace-cmd/trace-cmd.h @@ -389,6 +389,10 @@ struct hook_list { struct hook_list *tracecmd_create_event_hook(const char *arg); void tracecmd_free_hooks(struct hook_list *hooks); +void tracecmd_plog(const char *fmt, ...); +void tracecmd_plog_error(const char *fmt, ...); +int tracecmd_set_logfile(char *logfile); + /* --- Hack! --- */ int tracecmd_blk_hack(struct tracecmd_input *handle); diff --git a/include/trace-cmd/trace-msg.h b/include/trace-cmd/trace-msg.h index b7fe10b..aab8a69 100644 --- a/include/trace-cmd/trace-msg.h +++ b/include/trace-cmd/trace-msg.h @@ -12,7 +12,4 @@ extern unsigned int page_size; -void plog(const char *fmt, ...); -void pdie(const char *fmt, ...); - #endif /* _TRACE_MSG_H_ */ diff --git a/lib/trace-cmd/trace-msg.c b/lib/trace-cmd/trace-msg.c index 92562c7..74c5254 100644 --- a/lib/trace-cmd/trace-msg.c +++ b/lib/trace-cmd/trace-msg.c @@ -318,7 +318,7 @@ static int tracecmd_msg_recv(int fd, struct tracecmd_msg *msg) return 0; error: - plog("Receive an invalid message(size=%d)\n", size); + tracecmd_plog("Receive an invalid message(size=%d)\n", size); return -ENOMSG; } @@ -531,7 +531,7 @@ int tracecmd_msg_initial_setting(struct tracecmd_msg_handle *msg_handle) } cpus = ntohl(msg.tinit.cpus); - plog("cpus=%d\n", cpus); + tracecmd_plog("cpus=%d\n", cpus); if (cpus < 0) { ret = -EINVAL; goto error; @@ -540,7 +540,7 @@ int tracecmd_msg_initial_setting(struct tracecmd_msg_handle *msg_handle) msg_handle->cpu_count = cpus; pagesize = ntohl(msg.tinit.page_size); - plog("pagesize=%d\n", pagesize); + tracecmd_plog("pagesize=%d\n", pagesize); if (pagesize <= 0) { ret = -EINVAL; goto error; @@ -570,7 +570,7 @@ int tracecmd_msg_initial_setting(struct tracecmd_msg_handle *msg_handle) /* do we understand this option? */ if (!process_option(msg_handle, p)) - plog("Cannot understand option '%s'\n", p); + tracecmd_plog("Cannot understand option '%s'\n", p); p = strchr(p, '\0'); } diff --git a/lib/trace-cmd/trace-util.c b/lib/trace-cmd/trace-util.c index faff0b5..60bcb68 100644 --- a/lib/trace-cmd/trace-util.c +++ b/lib/trace-cmd/trace-util.c @@ -30,6 +30,8 @@ int tracecmd_disable_sys_plugins; int tracecmd_disable_plugins; static bool tracecmd_debug; +static FILE *logfp; + static struct registered_plugin_options { struct registered_plugin_options *next; struct tep_plugin_option *options; @@ -1695,3 +1697,71 @@ void __weak *malloc_or_die(unsigned int size) die("malloc"); return data; } + +#define LOG_BUF_SIZE 1024 +static void __plog(const char *prefix, const char *fmt, va_list ap, FILE *fp) +{ + static int newline = 1; + char buf[LOG_BUF_SIZE]; + int r; + + r = vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); + + if (r > LOG_BUF_SIZE) + r = LOG_BUF_SIZE; + + if (logfp) { + if (newline) + fprintf(logfp, "[%d]%s%.*s", getpid(), prefix, r, buf); + else + fprintf(logfp, "[%d]%s%.*s", getpid(), prefix, r, buf); + newline = buf[r - 1] == '\n'; + fflush(logfp); + return; + } + + fprintf(fp, "%.*s", r, buf); +} + +void tracecmd_plog(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + __plog("", fmt, ap, stdout); + va_end(ap); + /* Make sure it gets to the screen, in case we crash afterward */ + fflush(stdout); +} + +void tracecmd_plog_error(const char *fmt, ...) +{ + va_list ap; + char *str = ""; + + va_start(ap, fmt); + __plog("Error: ", fmt, ap, stderr); + va_end(ap); + if (errno) + str = strerror(errno); + if (logfp) + fprintf(logfp, "\n%s\n", str); + else + fprintf(stderr, "\n%s\n", str); +} + +/** + * tracecmd_set_logfile - Set file for logging + * @logfile: Name of the log file + * + * Returns 0 on successful completion or -1 in case of error + */ +int tracecmd_set_logfile(char *logfile) +{ + if (logfp) + fclose(logfp); + logfp = fopen(logfile, "w"); + if (!logfp) + return -1; + return 0; +} diff --git a/tracecmd/trace-listen.c b/tracecmd/trace-listen.c index 9dcb833..233d661 100644 --- a/tracecmd/trace-listen.c +++ b/tracecmd/trace-listen.c @@ -34,8 +34,6 @@ static char *output_dir; static char *default_output_file = "trace"; static char *output_file; -static FILE *logfp; - static int backlog = 5; static int do_daemon; @@ -44,6 +42,13 @@ static int do_daemon; static struct tracecmd_msg_handle *stop_msg_handle; static bool done; +#define pdie(fmt, ...) \ + do { \ + tracecmd_plog_error(fmt, ##__VA_ARGS__);\ + remove_pid_file(); \ + exit(-1); \ + } while (0) + #define TEMP_FILE_STR "%s.%s:%s.cpu%d", output_file, host, port, cpu static char *get_temp_file(const char *host, const char *port, int cpu) { @@ -114,43 +119,6 @@ static void finish(int sig) done = true; } -#define LOG_BUF_SIZE 1024 -static void __plog(const char *prefix, const char *fmt, va_list ap, - FILE *fp) -{ - static int newline = 1; - char buf[LOG_BUF_SIZE]; - int r; - - r = vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); - - if (r > LOG_BUF_SIZE) - r = LOG_BUF_SIZE; - - if (logfp) { - if (newline) - fprintf(logfp, "[%d]%s%.*s", getpid(), prefix, r, buf); - else - fprintf(logfp, "[%d]%s%.*s", getpid(), prefix, r, buf); - newline = buf[r - 1] == '\n'; - fflush(logfp); - return; - } - - fprintf(fp, "%.*s", r, buf); -} - -void plog(const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - __plog("", fmt, ap, stdout); - va_end(ap); - /* Make sure it gets to the screen, in case we crash afterward */ - fflush(stdout); -} - static void make_pid_name(int mode, char *buf) { snprintf(buf, PATH_MAX, VAR_RUN_DIR "/trace-cmd-net.pid"); @@ -169,26 +137,6 @@ static void remove_pid_file(void) unlink(buf); } -void pdie(const char *fmt, ...) -{ - va_list ap; - char *str = ""; - - va_start(ap, fmt); - __plog("Error: ", fmt, ap, stderr); - va_end(ap); - if (errno) - str = strerror(errno); - if (logfp) - fprintf(logfp, "\n%s\n", str); - else - fprintf(stderr, "\n%s\n", str); - - remove_pid_file(); - - exit(-1); -} - static int process_udp_child(int sfd, const char *host, const char *port, int cpu, int page_size, int use_tcp) { @@ -369,15 +317,15 @@ static int communicate_with_client(struct tracecmd_msg_handle *msg_handle) if (memcmp(buf, V3_CPU, n) != 0) { /* If it did not send a version, then bail */ if (memcmp(buf, "-1V", 3)) { - plog("Unknown string %s\n", buf); + tracecmd_plog("Unknown string %s\n", buf); goto out; } /* Skip "-1" */ - plog("Cannot handle the protocol %s\n", buf+2); + tracecmd_plog("Cannot handle the protocol %s\n", buf+2); /* If it returned the same command as last time, bail! */ if (last_proto && strncmp(last_proto, buf, n) == 0) { - plog("Repeat of version %s sent\n", last_proto); + tracecmd_plog("Repeat of version %s sent\n", last_proto); goto out; } free(last_proto); @@ -410,7 +358,7 @@ static int communicate_with_client(struct tracecmd_msg_handle *msg_handle) } else { /* The client is using the v1 protocol */ - plog("cpus=%d\n", cpus); + tracecmd_plog("cpus=%d\n", cpus); if (cpus < 0) goto out; @@ -424,7 +372,7 @@ static int communicate_with_client(struct tracecmd_msg_handle *msg_handle) pagesize = atoi(buf); - plog("pagesize=%d\n", pagesize); + tracecmd_plog("pagesize=%d\n", pagesize); if (pagesize <= 0) goto out; @@ -473,7 +421,7 @@ static int communicate_with_client(struct tracecmd_msg_handle *msg_handle) } if (msg_handle->flags & TRACECMD_MSG_FL_USE_TCP) - plog("Using TCP for live connection\n"); + tracecmd_plog("Using TCP for live connection\n"); ret = pagesize; out: @@ -560,7 +508,7 @@ static int *create_all_readers(const char *node, const char *port, if (msg_handle->version == V3_PROTOCOL) { /* send set of port numbers to the client */ if (tracecmd_msg_send_port_array(msg_handle, port_array) < 0) { - plog("Failed sending port array\n"); + tracecmd_plog("Failed sending port array\n"); goto out_free; } } else { @@ -755,11 +703,9 @@ static int do_connection(int cfd, struct sockaddr_storage *peer_addr, service, NI_MAXSERV, NI_NUMERICSERV); if (s == 0) - plog("Connected with %s:%s\n", - host, service); + tracecmd_plog("Connected with %s:%s\n", host, service); else { - plog("Error with getnameinfo: %s\n", - gai_strerror(s)); + tracecmd_plog("Error with getnameinfo: %s\n", gai_strerror(s)); close(cfd); tracecmd_msg_handle_close(msg_handle); return -1; @@ -1030,8 +976,7 @@ void trace_listen(int argc, char **argv) if (logfile) { /* set the writes to a logfile instead */ - logfp = fopen(logfile, "w"); - if (!logfp) + if (tracecmd_set_logfile(logfile) < 0) die("creating log file %s", logfile); } diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index 61457c8..96d2c1a 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -3055,7 +3055,7 @@ static void check_protocol_version(struct tracecmd_msg_handle *msg_handle) if (n < 0 || !buf[0]) { /* the server uses the v1 protocol, so we'll use it */ msg_handle->version = V1_PROTOCOL; - plog("Use the v1 protocol\n"); + tracecmd_plog("Use the v1 protocol\n"); } else { if (memcmp(buf, "V3", n) != 0) die("Cannot handle the protocol %s", buf); From patchwork Tue Sep 3 13:34:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 11127903 X-Patchwork-Delegate: rostedt@goodmis.org Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6009E16B1 for ; Tue, 3 Sep 2019 13:34:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 40C7423697 for ; Tue, 3 Sep 2019 13:34:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hRA8P6DC" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725782AbfICNex (ORCPT ); Tue, 3 Sep 2019 09:34:53 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:45136 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729079AbfICNew (ORCPT ); Tue, 3 Sep 2019 09:34:52 -0400 Received: by mail-wr1-f67.google.com with SMTP id q12so17484700wrj.12 for ; Tue, 03 Sep 2019 06:34:50 -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=Voz7nvReBY24TLcA5RUoWGWvD5uFPu6TGoUNfcBjLiM=; b=hRA8P6DCtBCaAiCtZuUkO+0FylmaIXPBwwZU1qykvhbHWFJVKUPhOCsqkAwgupg9Fo cx32M4peoNkNitJYin3z3rKR0zgfC5+TCieqZa6mNxyxzBZ2nDX1Ci5FnZXr9LvWB4j2 fWm3/9FDLu/C9qBmQAF+3Ooo2P3gLHxeAiP75ifulRglUF5TPO7zzvWgc/h2aACWYEnB q6Elf3A4Ayqvi1WTh04d7IhleWiX3y3OGvM6JcgdpSPNilq+sVrLLmP4wV8QBNqHbB2E 1sa0XJ2eMTRtMy1btytwoJXsYSafpm0/gWxXogtDX9Ehx5H0aKJuTbYHD+WDaL3Rqs0A F99w== 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=Voz7nvReBY24TLcA5RUoWGWvD5uFPu6TGoUNfcBjLiM=; b=HmXZJh/IeSOnpHe077z6vpK1a8Z8UTKd4Gx3s+W12jrJ46IgBzVEnoELTZD7IJxpTZ od6jPU9h6LN0pD/KZzWXZIYWIVs9+To5DOH+27kaME/KHNVY3mtsmmQpUks8j+o4+h8p V2Fl8GPcf4u6lHShuPYi5VkQZvNPy+9le+0IE7a0K94Qbs7KcFS08YZUOa74Dht2V2/P R8JNmckSgdWe7MohTE2fhIxlqJ+sAWjGN3A88HVZ6bKQdLlrWls3lEXB5c3mQwTS2l0x DAAexOy610iPZ9Qiwp7G3Nq4EdtUbLovVjuvWns5+I9kerCbdzX1SsJh92fIIlMWRHJl Vq5A== X-Gm-Message-State: APjAAAW0M4YIZxissxE3tTiF4jjsTcWEejTw+8GbVFbpTdaYmE8VUVfg T0w3s7E3A+N/kycjwitEp4DOuVxJ X-Google-Smtp-Source: APXvYqxkN3zzYhzAshh11zYVokpWaJUBUPqgHebX9kfkrdd9QjiMju/ivSMfTTHqANDIr5/fD2+MyA== X-Received: by 2002:a5d:574c:: with SMTP id q12mr14323600wrw.69.1567517690026; Tue, 03 Sep 2019 06:34:50 -0700 (PDT) Received: from oberon.eng.vmware.com ([146.247.46.5]) by smtp.gmail.com with ESMTPSA id r16sm33509833wrc.81.2019.09.03.06.34.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2019 06:34:49 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 3/4] trace-cmd: Move tracecmd_stack_tracer_status() function to libtracecmd Date: Tue, 3 Sep 2019 16:34:44 +0300 Message-Id: <20190903133445.30486-4-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190903133445.30486-1-tz.stoyanov@gmail.com> References: <20190903133445.30486-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Sender: linux-trace-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org tracecmd_stack_tracer_status() function reads the stack tracer status from the proc file system. It does not depend on trace-cmd context and can be used standalone. The function is moved from trace-cmd application into libtracecmd. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-util.c | 51 ++++++++++++++++++++++++++++++++++ tracecmd/trace-stack.c | 56 ++------------------------------------ 2 files changed, 53 insertions(+), 54 deletions(-) diff --git a/lib/trace-cmd/trace-util.c b/lib/trace-cmd/trace-util.c index 60bcb68..6e2a352 100644 --- a/lib/trace-cmd/trace-util.c +++ b/lib/trace-cmd/trace-util.c @@ -25,6 +25,7 @@ #define LOCAL_PLUGIN_DIR ".trace-cmd/plugins" #define TRACEFS_PATH "/sys/kernel/tracing" #define DEBUGFS_PATH "/sys/kernel/debug" +#define PROC_STACK_FILE "/proc/sys/kernel/stack_tracer_enabled" int tracecmd_disable_sys_plugins; int tracecmd_disable_plugins; @@ -1765,3 +1766,53 @@ int tracecmd_set_logfile(char *logfile) return -1; return 0; } + +/** + * tracecmd_stack_tracer_status - Check stack trace status + * @status: Returned stack trace status: + * 0 - not configured, disabled + * non 0 - enabled + * + * Returns -1 in case of an error, 0 if file does not exist + * (stack tracer not configured in kernel) or 1 on successful completion. + */ +int tracecmd_stack_tracer_status(int *status) +{ + struct stat stat_buf; + char buf[64]; + long num; + int fd; + int n; + + if (stat(PROC_STACK_FILE, &stat_buf) < 0) { + /* stack tracer not configured on running kernel */ + *status = 0; /* not configured means disabled */ + return 0; + } + + fd = open(PROC_STACK_FILE, O_RDONLY); + + if (fd < 0) + return -1; + + n = read(fd, buf, sizeof(buf)); + close(fd); + + if (n <= 0) + return -1; + + if (n >= sizeof(buf)) + return -1; + + buf[n] = 0; + + errno = 0; + num = strtol(buf, NULL, 10); + + /* Check for various possible errors */ + if (num > INT_MAX || num < INT_MIN || (!num && errno)) + return -1; + + *status = num; + return 1; /* full success */ +} diff --git a/tracecmd/trace-stack.c b/tracecmd/trace-stack.c index 34b3b58..bb002c0 100644 --- a/tracecmd/trace-stack.c +++ b/tracecmd/trace-stack.c @@ -36,58 +36,6 @@ static void test_available(void) die("stack tracer not configured on running kernel"); } -/* - * Returns: - * -1 - Something went wrong - * 0 - File does not exist (stack tracer not enabled) - * 1 - Success - */ -static int read_proc(int *status) -{ - struct stat stat_buf; - char buf[64]; - long num; - int fd; - int n; - - if (stat(PROC_FILE, &stat_buf) < 0) { - /* stack tracer not configured on running kernel */ - *status = 0; /* not configured means disabled */ - return 0; - } - - fd = open(PROC_FILE, O_RDONLY); - - if (fd < 0) - return -1; - - n = read(fd, buf, sizeof(buf)); - close(fd); - - if (n <= 0) - return -1; - - if (n >= sizeof(buf)) - return -1; - - buf[n] = 0; - errno = 0; - num = strtol(buf, NULL, 10); - - /* Check for various possible errors */ - if (num > INT_MAX || num < INT_MIN || (!num && errno)) - return -1; - - *status = num; - return 1; /* full success */ -} - -/* Public wrapper of read_proc() */ -int tracecmd_stack_tracer_status(int *status) -{ - return read_proc(status); -} - /* NOTE: this implementation only accepts new_status in the range [0..9]. */ static void change_stack_tracer_status(unsigned new_status) { @@ -102,7 +50,7 @@ static void change_stack_tracer_status(unsigned new_status) return; } - ret = read_proc(&status); + ret = tracecmd_stack_tracer_status(&status); if (ret < 0) die("error reading %s", PROC_FILE); @@ -160,7 +108,7 @@ static void read_trace(void) size_t n; int r; - if (read_proc(&status) <= 0) + if (tracecmd_stack_tracer_status(&status) <= 0) die("Invalid stack tracer state"); if (status > 0) From patchwork Tue Sep 3 13:34:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 11127905 X-Patchwork-Delegate: rostedt@goodmis.org Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9F0A31399 for ; Tue, 3 Sep 2019 13:34:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7F2CD2343A for ; Tue, 3 Sep 2019 13:34:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dalcjEJz" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729079AbfICNex (ORCPT ); Tue, 3 Sep 2019 09:34:53 -0400 Received: from mail-wm1-f68.google.com ([209.85.128.68]:35811 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729097AbfICNex (ORCPT ); Tue, 3 Sep 2019 09:34:53 -0400 Received: by mail-wm1-f68.google.com with SMTP id n10so7620092wmj.0 for ; Tue, 03 Sep 2019 06:34:51 -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=KlyJC//4t9uyXIA+1WUyo21KizWqqhloWOCnki7piOA=; b=dalcjEJz5v0eKvS6CTNmbBlIw37O1lJv9+Qmj3PccdYukR+JhRyE5bnoVPm5muyQOn uYiDBnt60xhGAvvRoWQwgY+DuQ3XvG67rdvqEgGdOovFQ8SIBtEkMm4hxkuxPIRfJM3o EPgPo/MRWUfl59LfWLFadhBF/I7EocOtmQXL6ImzMKgtXoqQg5LI7ObbFqRI6p+BvHMu uJolVImjBMFPcwUajXzbCBgXM/PjZqI8eroVs8g13NcRJ3KJfhaDsXjwikN7oUHhYxOU nmn6AmemOcSPmS/MnOWEHZVW0hCqDYM38zF+crzUWqf+62hZ3EuqJfywfSADhSH+7NZZ vybw== 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=KlyJC//4t9uyXIA+1WUyo21KizWqqhloWOCnki7piOA=; b=KjqgUW3qaSfKWIP+TnW8qv6/sk43Rv1paAe1C7qmVEDqBA+uJbhHgYPh8UV89u3cQK mBiffbrhl01vstaRjRf89F36X1Do5cA5Gs5/68TFxGGeP5el1dssVCOddD874QogfaQZ WkC/FOOhAJCk62kVAut4/0ceVNvtxHDCNYEzTNwrk7MpD/SmTfdGesLH6VIZh9VCHonL vNb1dZurY+AU/iGuZfFG4dWIjZDWvR4q9hzQlSw4fNfW4G9bB2lQ0LLhtp/PCiVrysPj ELrxMMuTDXq3fjekcjMtdO1LjaBt7pJ4h+O52Aa+g+awo7lQm7Wz9ydE5Rt1BSPm4L6M kjRA== X-Gm-Message-State: APjAAAXneKbdUXaq0kYMBHUDxffsrB4GOfqIE7X6xFblF5SBVsN/FQl6 KUUYc/px4Y7iU7XNGfK0GwymVls7 X-Google-Smtp-Source: APXvYqxg45ga8MVNwAcXiiJs1cEtkXNCpvcGeHgCWAceZyEGFI7Iw+3cuNkIc8XX9GXuVE8bQML91g== X-Received: by 2002:a7b:c761:: with SMTP id x1mr67347wmk.100.1567517690960; Tue, 03 Sep 2019 06:34:50 -0700 (PDT) Received: from oberon.eng.vmware.com ([146.247.46.5]) by smtp.gmail.com with ESMTPSA id r16sm33509833wrc.81.2019.09.03.06.34.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2019 06:34:50 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 4/4] trace-cmd: Update descriptions of "debug" libtracecmd APIs Date: Tue, 3 Sep 2019 16:34:45 +0300 Message-Id: <20190903133445.30486-5-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190903133445.30486-1-tz.stoyanov@gmail.com> References: <20190903133445.30486-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Sender: linux-trace-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org The description of "debug" libtracecmd APIs: tracecmd_set_debug() tracecmd_get_debug() does not match the functions. There was a copy leftover from "quiet" APIs. The name of the local static variable is changed form tracecmd_debug to debug, as it is not visible outside of the library. Signed-off-by: Tzvetomir Stoyanov (VMware) --- include/trace-cmd/trace-cmd.h | 2 +- lib/trace-cmd/trace-util.c | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/trace-cmd/trace-cmd.h b/include/trace-cmd/trace-cmd.h index de3183d..3c5e827 100644 --- a/include/trace-cmd/trace-cmd.h +++ b/include/trace-cmd/trace-cmd.h @@ -49,7 +49,7 @@ enum { void tracecmd_record_ref(struct tep_record *record); void free_record(struct tep_record *record); -void tracecmd_set_debug(bool debug); +void tracecmd_set_debug(bool set_debug); bool tracecmd_get_debug(void); struct tracecmd_input; diff --git a/lib/trace-cmd/trace-util.c b/lib/trace-cmd/trace-util.c index 6e2a352..6a53004 100644 --- a/lib/trace-cmd/trace-util.c +++ b/lib/trace-cmd/trace-util.c @@ -29,7 +29,7 @@ int tracecmd_disable_sys_plugins; int tracecmd_disable_plugins; -static bool tracecmd_debug; +static bool debug; static FILE *logfp; @@ -101,23 +101,23 @@ char **trace_util_list_plugin_options(void) } /** - * tracecmd_set_quiet - Set if to print output to the screen - * @quiet: If non zero, print no output to the screen - * + * tracecmd_set_debug - Set debug mode of the tracecmd library + * @set_debug: The new "debug" mode. If true, the tracecmd library is + * in "debug" mode */ -void tracecmd_set_debug(bool debug) +void tracecmd_set_debug(bool set_debug) { - tracecmd_debug = debug; + debug = set_debug; } /** - * tracecmd_get_quiet - Get if to print output to the screen - * Returns non zero, if no output to the screen should be printed + * tracecmd_get_debug - Get debug mode of tracecmd library + * Returns true, if the tracecmd library is in debug mode. * */ bool tracecmd_get_debug(void) { - return tracecmd_debug; + return debug; } void trace_util_free_plugin_options_list(char **list)