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 tracecmd_lib_fatal() library 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. The tracecmd_lib_fatal() function is defined as weak and can be overridden by the application. Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@xxxxxxxxx> --- v4 changes: - Renamed _lib_fatal() to tracecmd_lib_fatal() and implemented it as a weak function. 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 | 1 + lib/trace-cmd/trace-input.c | 29 +++++++++++------ lib/trace-cmd/trace-util.c | 41 +++++++++---------------- 3 files changed, 35 insertions(+), 36 deletions(-) diff --git a/lib/trace-cmd/include/trace-cmd-local.h b/lib/trace-cmd/include/trace-cmd-local.h index 0cd27441..376aff11 100644 --- a/lib/trace-cmd/include/trace-cmd-local.h +++ b/lib/trace-cmd/include/trace-cmd-local.h @@ -11,6 +11,7 @@ /* Can be overridden */ void warning(const char *fmt, ...); +void tracecmd_lib_fatal(const char *fmt, ...); /* trace.dat file format version */ #define FILE_VERSION 6 diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 2f7ba5db..7b25e92c 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" @@ -1102,8 +1103,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) { + tracecmd_lib_fatal("Page ref count is zero!\n"); + return; + } page->ref_count--; if (page->ref_count) @@ -1161,16 +1164,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) { + tracecmd_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) { + tracecmd_lib_fatal("freeing record when it is locked!"); + return; + } record->data = NULL; @@ -1370,7 +1377,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); + tracecmd_lib_fatal("bad page offset %llx", offset); return -1; } @@ -1378,7 +1385,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); + tracecmd_lib_fatal("bad page offset %llx", offset); return -1; } @@ -1943,8 +1950,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) { + tracecmd_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 9e37b371..db7bead6 100644 --- a/lib/trace-cmd/trace-util.c +++ b/lib/trace-cmd/trace-util.c @@ -353,48 +353,35 @@ trace_load_plugins(struct tep_handle *tep, int flags) return list; } -void __noreturn __vdie(const char *fmt, va_list ap) +static int __vlib_warning(const char *fmt, va_list ap) { int ret = errno; if (errno) - perror("trace-cmd"); - else - ret = -1; + perror("libtracecmd"); fprintf(stderr, " "); 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); + return ret; } -void __weak __noreturn die(const char *fmt, ...) +void __weak tracecmd_lib_fatal(const char *fmt, ...) { + int ret; va_list ap; va_start(ap, fmt); - __vdie(fmt, ap); + ret = __vlib_warning(fmt, ap); va_end(ap); -} - -void __weak *malloc_or_die(unsigned int size) -{ - void *data; - data = malloc(size); - if (!data) - die("malloc"); - return data; + if (debug) { + if (!ret) + ret = -1; + exit(ret); + } } #define LOG_BUF_SIZE 1024 @@ -546,8 +533,10 @@ int tracecmd_count_cpus(void) pbuf = buf; fp = fopen("/proc/cpuinfo", "r"); - if (!fp) - die("Can not read cpuinfo"); + if (!fp) { + tracecmd_lib_fatal("Can not read cpuinfo"); + return 0; + } while ((r = getline(&pbuf, pn, fp)) >= 0) { char *p; -- 2.30.2