Linux-Trace-Devel Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH v3] trace-cmd: Remove all die()s from trace-cmd library
@ 2021-03-25  8:39 Tzvetomir Stoyanov (VMware)
  2021-03-26 18:22 ` Steven Rostedt
  0 siblings, 1 reply; 2+ messages in thread
From: Tzvetomir Stoyanov (VMware) @ 2021-03-25  8:39 UTC (permalink / raw)
  To: rostedt; +Cc: linux-trace-devel

The die() call is used for a fatal error. It terminates the current
program with exit(). The program should not be terminated from a library
routine, the die() call should not be used in the trace-cmd library
context.

But as all these logs indicate a fatal error in the library, it is
useful to have them. The die() call is replaced with _lib_fatal()
library internal function, which prints the error message. By default,
the application is not terminated when a fatal error is encountered in
the library. If the library runs in debug mode, the _lib_fatal() call
behaves like die() - calls exit() to terminate the application.
The library debug mode can be switched using tracecmd_set_debug() API.

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
v3 changes:
 - If the library runs in debug mode, call exit() in _lib_fatal().  
v2 changes:
 - Replaced die() with _lib_fatal().

 lib/trace-cmd/include/trace-cmd-local.h |  2 ++
 lib/trace-cmd/trace-input.c             | 29 +++++++++++++-------
 lib/trace-cmd/trace-util.c              | 35 +++++++------------------
 3 files changed, 31 insertions(+), 35 deletions(-)

diff --git a/lib/trace-cmd/include/trace-cmd-local.h b/lib/trace-cmd/include/trace-cmd-local.h
index d265e4c8..da9d8d58 100644
--- a/lib/trace-cmd/include/trace-cmd-local.h
+++ b/lib/trace-cmd/include/trace-cmd-local.h
@@ -12,6 +12,8 @@
 /* Can be overridden */
 void warning(const char *fmt, ...);
 
+void _lib_fatal(const char *fmt, ...);
+
 #define __packed __attribute__((packed))
 
 /* trace.dat file format version */
diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c
index 2093a3dc..3687de09 100644
--- a/lib/trace-cmd/trace-input.c
+++ b/lib/trace-cmd/trace-input.c
@@ -16,6 +16,7 @@
 #include <linux/time64.h>
 
 #include "trace-write-local.h"
+#include "trace-cmd-local.h"
 #include "trace-local.h"
 #include "kbuffer.h"
 #include "list.h"
@@ -1087,8 +1088,10 @@ static void __free_page(struct tracecmd_input *handle, struct page *page)
 	struct page **pages;
 	int index;
 
-	if (!page->ref_count)
-		die("Page ref count is zero!\n");
+	if (!page->ref_count) {
+		_lib_fatal("Page ref count is zero!\n");
+		return;
+	}
 
 	page->ref_count--;
 	if (page->ref_count)
@@ -1146,16 +1149,20 @@ void tracecmd_free_record(struct tep_record *record)
 	if (!record)
 		return;
 
-	if (!record->ref_count)
-		die("record ref count is zero!");
+	if (!record->ref_count) {
+		_lib_fatal("record ref count is zero!");
+		return;
+	}
 
 	record->ref_count--;
 
 	if (record->ref_count)
 		return;
 
-	if (record->locked)
-		die("freeing record when it is locked!");
+	if (record->locked) {
+		_lib_fatal("freeing record when it is locked!");
+		return;
+	}
 
 	record->data = NULL;
 
@@ -1318,7 +1325,7 @@ static int get_page(struct tracecmd_input *handle, int cpu,
 
 	if (offset & (handle->page_size - 1)) {
 		errno = -EINVAL;
-		die("bad page offset %llx", offset);
+		_lib_fatal("bad page offset %llx", offset);
 		return -1;
 	}
 
@@ -1326,7 +1333,7 @@ static int get_page(struct tracecmd_input *handle, int cpu,
 	    offset > handle->cpu_data[cpu].file_offset +
 	    handle->cpu_data[cpu].file_size) {
 		errno = -EINVAL;
-		die("bad page offset %llx", offset);
+		_lib_fatal("bad page offset %llx", offset);
 		return -1;
 	}
 
@@ -1891,8 +1898,10 @@ tracecmd_peek_data(struct tracecmd_input *handle, int cpu)
 	if (handle->cpu_data[cpu].next) {
 
 		record = handle->cpu_data[cpu].next;
-		if (!record->data)
-			die("Something freed the record");
+		if (!record->data) {
+			_lib_fatal("Something freed the record");
+			return NULL;
+		}
 
 		if (handle->cpu_data[cpu].timestamp == record->ts)
 			return record;
diff --git a/lib/trace-cmd/trace-util.c b/lib/trace-cmd/trace-util.c
index 538adbc2..cb397452 100644
--- a/lib/trace-cmd/trace-util.c
+++ b/lib/trace-cmd/trace-util.c
@@ -353,12 +353,12 @@ trace_load_plugins(struct tep_handle *tep, int flags)
 	return list;
 }
 
-void __noreturn __vdie(const char *fmt, va_list ap)
+void __vlib_fatal(const char *fmt, va_list ap)
 {
 	int ret = errno;
 
 	if (errno)
-		perror("trace-cmd");
+		perror("libtracecmd");
 	else
 		ret = -1;
 
@@ -366,37 +366,20 @@ void __noreturn __vdie(const char *fmt, va_list ap)
 	vfprintf(stderr, fmt, ap);
 
 	fprintf(stderr, "\n");
-	exit(ret);
-}
-
-void __noreturn __die(const char *fmt, ...)
-{
-	va_list ap;
 
-	va_start(ap, fmt);
-	__vdie(fmt, ap);
-	va_end(ap);
+	if (debug)
+		exit(ret);
 }
 
-void __weak __noreturn die(const char *fmt, ...)
+void _lib_fatal(const char *fmt, ...)
 {
 	va_list ap;
 
 	va_start(ap, fmt);
-	__vdie(fmt, ap);
+	__vlib_fatal(fmt, ap);
 	va_end(ap);
 }
 
-void __weak *malloc_or_die(unsigned int size)
-{
-	void *data;
-
-	data = malloc(size);
-	if (!data)
-		die("malloc");
-	return data;
-}
-
 #define LOG_BUF_SIZE 1024
 static void __plog(const char *prefix, const char *fmt, va_list ap, FILE *fp)
 {
@@ -546,8 +529,10 @@ int tracecmd_count_cpus(void)
 	pbuf = buf;
 
 	fp = fopen("/proc/cpuinfo", "r");
-	if (!fp)
-		die("Can not read cpuinfo");
+	if (!fp) {
+		_lib_fatal("Can not read cpuinfo");
+		return 0;
+	}
 
 	while ((r = getline(&pbuf, pn, fp)) >= 0) {
 		char *p;
-- 
2.30.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v3] trace-cmd: Remove all die()s from trace-cmd library
  2021-03-25  8:39 [PATCH v3] trace-cmd: Remove all die()s from trace-cmd library Tzvetomir Stoyanov (VMware)
@ 2021-03-26 18:22 ` Steven Rostedt
  0 siblings, 0 replies; 2+ messages in thread
From: Steven Rostedt @ 2021-03-26 18:22 UTC (permalink / raw)
  To: Tzvetomir Stoyanov (VMware); +Cc: linux-trace-devel

On Thu, 25 Mar 2021 10:39:32 +0200
"Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com> wrote:

> @@ -353,12 +353,12 @@ trace_load_plugins(struct tep_handle *tep, int flags)
>  	return list;
>  }
>  
> -void __noreturn __vdie(const char *fmt, va_list ap)
> +void __vlib_fatal(const char *fmt, va_list ap)
>  {
>  	int ret = errno;
>  
>  	if (errno)
> -		perror("trace-cmd");
> +		perror("libtracecmd");
>  	else
>  		ret = -1;
>  


> -void __weak __noreturn die(const char *fmt, ...)
> +void _lib_fatal(const char *fmt, ...)

Looks good, but I wonder if we should make these functions hidden, or at
least rename them to: tracecmd_lib_fatal(), and also make them weak, to
allow applications to override them (display pop ups, like KernelShark can
do).

-- Steve


>  {
>  	va_list ap;
>  
>  	va_start(ap, fmt);
> -	__vdie(fmt, ap);
> +	__vlib_fatal(fmt, ap);
>  	va_end(ap);
>  }
>  

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-25  8:39 [PATCH v3] trace-cmd: Remove all die()s from trace-cmd library Tzvetomir Stoyanov (VMware)
2021-03-26 18:22 ` Steven Rostedt

Linux-Trace-Devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-trace-devel/0 linux-trace-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-trace-devel linux-trace-devel/ https://lore.kernel.org/linux-trace-devel \
		linux-trace-devel@vger.kernel.org
	public-inbox-index linux-trace-devel

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-trace-devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git