All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com>
To: rostedt@goodmis.org
Cc: linux-trace-devel@vger.kernel.org
Subject: [PATCH] libtracefs: Implement tracefs_warning()
Date: Wed,  7 Apr 2021 08:11:54 +0300	[thread overview]
Message-ID: <20210407051154.2422172-1-tz.stoyanov@gmail.com> (raw)

The warning() function is used in a lot of places in the libracefs
library, and it is implemented as a dummy weak function. There is a
weak implementation of the same function in traceevent library, which
could cause a problem when both are used in an application.
Replaced warning() with tracefs_warning() and implemented it as a
weak function, specific to this library.
Added a __weak define in the library internal header file.

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 include/tracefs-local.h |  3 ++-
 src/tracefs-events.c    |  2 +-
 src/tracefs-instance.c  |  8 ++++----
 src/tracefs-utils.c     | 31 ++++++++++++++++++++++++++-----
 4 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/include/tracefs-local.h b/include/tracefs-local.h
index 73ec113..5a69ef7 100644
--- a/include/tracefs-local.h
+++ b/include/tracefs-local.h
@@ -7,6 +7,7 @@
 #define _TRACE_FS_LOCAL_H
 
 #define __hidden __attribute__((visibility ("hidden")))
+#define __weak __attribute__((weak))
 
 #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
 
@@ -22,7 +23,7 @@ struct tracefs_instance {
 };
 
 /* Can be overridden */
-void warning(const char *fmt, ...);
+void tracefs_warning(const char *fmt, ...);
 
 int str_read_file(const char *file, char **buffer);
 char *trace_append_file(const char *dir, const char *name);
diff --git a/src/tracefs-events.c b/src/tracefs-events.c
index da56943..9a706ab 100644
--- a/src/tracefs-events.c
+++ b/src/tracefs-events.c
@@ -43,7 +43,7 @@ page_to_kbuf(struct tep_handle *tep, void *page, int size)
 
 	kbuffer_load_subbuffer(kbuf, page);
 	if (kbuffer_subbuffer_size(kbuf) > size) {
-		warning("%s: page_size > size", __func__);
+		tracefs_warning("%s: page_size > size", __func__);
 		kbuffer_free(kbuf);
 		kbuf = NULL;
 	}
diff --git a/src/tracefs-instance.c b/src/tracefs-instance.c
index bf2fabf..e87b360 100644
--- a/src/tracefs-instance.c
+++ b/src/tracefs-instance.c
@@ -203,7 +203,7 @@ int tracefs_instance_destroy(struct tracefs_instance *instance)
 	int ret = -1;
 
 	if (!instance || !instance->name) {
-		warning("Cannot remove top instance");
+		tracefs_warning("Cannot remove top instance");
 		return -1;
 	}
 
@@ -265,8 +265,8 @@ char *tracefs_instance_get_dir(struct tracefs_instance *instance)
 
 	ret = asprintf(&path, "%s/instances/%s", instance->trace_dir, instance->name);
 	if (ret < 0) {
-		warning("Failed to allocate path for instance %s",
-			 instance->name);
+		tracefs_warning("Failed to allocate path for instance %s",
+				instance->name);
 		return NULL;
 	}
 
@@ -308,7 +308,7 @@ static int write_file(const char *file, const char *str)
 
 	fd = open(file, O_WRONLY | O_TRUNC);
 	if (fd < 0) {
-		warning("Failed to open '%s'", file);
+		tracefs_warning("Failed to open '%s'", file);
 		return -1;
 	}
 	ret = write(fd, str, strlen(str));
diff --git a/src/tracefs-utils.c b/src/tracefs-utils.c
index 0e96f8e..9a70175 100644
--- a/src/tracefs-utils.c
+++ b/src/tracefs-utils.c
@@ -13,6 +13,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <errno.h>
 
 #include "tracefs.h"
 #include "tracefs-local.h"
@@ -23,8 +24,28 @@
 #define _STR(x) #x
 #define STR(x) _STR(x)
 
-void __attribute__((weak)) warning(const char *fmt, ...)
+static int __vlib_warning(const char *fmt, va_list ap)
 {
+	int ret = errno;
+
+	if (errno)
+		perror("libtracefs");
+
+	fprintf(stderr, "  ");
+	vfprintf(stderr, fmt, ap);
+
+	fprintf(stderr, "\n");
+
+	return ret;
+}
+
+void __weak tracefs_warning(const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	__vlib_warning(fmt, ap);
+	va_end(ap);
 }
 
 static int mount_tracefs(void)
@@ -76,7 +97,7 @@ __hidden char *trace_find_tracing_dir(void)
 
 	fp = fopen("/proc/mounts", "r");
 	if (!fp) {
-		warning("Can't open /proc/mounts for read");
+		tracefs_warning("Can't open /proc/mounts for read");
 		return NULL;
 	}
 
@@ -103,7 +124,7 @@ __hidden char *trace_find_tracing_dir(void)
 				fspath[PATH_MAX] = 0;
 			} else {
 				if (mount_debugfs() < 0) {
-					warning("debugfs not mounted, please mount");
+					tracefs_warning("debugfs not mounted, please mount");
 					free(debug_str);
 					return NULL;
 				}
@@ -200,7 +221,7 @@ __hidden int str_read_file(const char *file, char **buffer)
 
 	fd = open(file, O_RDONLY);
 	if (fd < 0) {
-		warning("File %s not found", file);
+		tracefs_warning("File %s not found", file);
 		return -1;
 	}
 
@@ -210,7 +231,7 @@ __hidden int str_read_file(const char *file, char **buffer)
 			continue;
 		nbuf = realloc(buf, size+r+1);
 		if (!nbuf) {
-			warning("Failed to allocate file buffer");
+			tracefs_warning("Failed to allocate file buffer");
 			size = -1;
 			break;
 		}
-- 
2.30.2


             reply	other threads:[~2021-04-07  5:12 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-07  5:11 Tzvetomir Stoyanov (VMware) [this message]
2021-04-07 16:19 ` [PATCH] libtracefs: Implement tracefs_warning() Steven Rostedt
2021-04-07 16:46   ` Tzvetomir Stoyanov
2021-04-07 16:54     ` Steven Rostedt
2021-04-07 16:55       ` Steven Rostedt
2021-04-07 16:59         ` Tzvetomir Stoyanov
2021-04-07 17:14           ` Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210407051154.2422172-1-tz.stoyanov@gmail.com \
    --to=tz.stoyanov@gmail.com \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.