trace-cmd: Implement warning() in the library
PATCH v3 1/1 trace-cmd: Implement warning() in the library
The warning() function is used in a lot of places in the trace-cmd library, but there is no implementation. The function is implemented in the trace-cmd application. There is also a weak implementation in traceevent library, which is specific to that library. Implemented a new tracecmd_lib_warning(), specific to the trace-cmd library and replaced all warning() calls with the new function in the library. The new function is implemented as weak, so it can be overridden by the application.
The tracecm_lib_warning() uses tep_vwarning() from libtraceevent for printing the warning, which is also a weak function and can be overridden by the application.
---
Depends on "[PATCH v2] libtraceevent: Add tep_warning()"
https://lore.kernel.org/linux-trace-devel/[email protected]/
v3 changes:
- Use tep_vwarning() for printing the warning message.
lib/trace-cmd/include/trace-cmd-local.h | 2 +- lib/trace-cmd/trace-hooks.c | 5 +- lib/trace-cmd/trace-input.c | 38 +++++----- lib/trace-cmd/trace-msg.c | 14 ++-- lib/trace-cmd/trace-output.c | 94 ++++++++++++------------- lib/trace-cmd/trace-plugin.c | 15 ++-- lib/trace-cmd/trace-recorder.c | 9 +-- lib/trace-cmd/trace-timesync.c | 3 +- lib/trace-cmd/trace-util.c | 26 +++---- python/ctracecmd.i | 2 +- tracecmd/include/trace-local.h | 2 + 11 files changed, 106 insertions(+), 104 deletions(-)
lib/trace-cmd/include/trace-cmd-local.h +1 -1
@@ -10,7 +10,7 @@#include "trace-cmd-private.h" /* Can be overridden */ -void warning (const char *fmt, ...); +void tracecmd_lib_warning (const char *fmt, ...);void tracecmd_lib_fatal (const char *fmt, ...);/* trace.dat file format version */
lib/trace-cmd/trace-hooks.c +3 -2
@@ -9,6 +9,7 @@#include <ctype.h> #include "trace-cmd-private.h" +#include "trace-cmd-local.h" #include "event-utils.h" struct hook_list *tracecmd_create_event_hook (const char *arg)
@@ -132,7 +133,7 @@ struct hook_list *tracecmd_create_event_hook(const char *arg) hook->stack =1 ;break ;default : -warning ("unknown flag %c\n" , flags[i]); +tracecmd_lib_warning ("unknown flag %c\n" , flags[i]); } } }
@@ -149,7 +150,7 @@ struct hook_list *tracecmd_create_event_hook(const char *arg)return hook; invalid_tok: -warning ("Invalid hook format '%s'" , arg); +tracecmd_lib_warning ("Invalid hook format '%s'" , arg);return NULL ; }
lib/trace-cmd/trace-input.c +19 -19
@@ -430,7 +430,7 @@ static int regex_event_buf(const char *file, int size, regex_t *epreg) buf =malloc (size +1 );if (!buf) { -warning ("Insufficient memory" ); +tracecmd_lib_warning ("Insufficient memory" );return 0 ; }
@@ -440,7 +440,7 @@ static int regex_event_buf(const char *file, int size, regex_t *epreg)/* get the name from the first line */ line =strtok (buf,"\n" );if (!line) { -warning ("No newline found in '%s'" , buf); +tracecmd_lib_warning ("No newline found in '%s'" , buf);return 0 ; }/* skip name if it is there */
@@ -543,13 +543,13 @@ static int make_preg_files(const char *regex, regex_t *system, ret =regcomp (system, sstr,REG_ICASE |REG_NOSUB );if (ret) { -warning ("Bad regular expression '%s'" , sstr); +tracecmd_lib_warning ("Bad regular expression '%s'" , sstr); goto out; } ret =regcomp (event, estr,REG_ICASE |REG_NOSUB );if (ret) { -warning ("Bad regular expression '%s'" , estr); +tracecmd_lib_warning ("Bad regular expression '%s'" , estr); goto out; }
@@ -1340,14 +1340,14 @@ static int update_page_info(struct tracecmd_input *handle, int cpu)/* FIXME: handle header page */ if (tep_get_header_timestamp_size (pevent) !=8 ) { -warning ("expected a long long type for timestamp" ); +tracecmd_lib_warning ("expected a long long type for timestamp" );return -1 ; }kbuffer_load_subbuffer (kbuf, ptr);if (kbuffer_subbuffer_size (kbuf) > handle->page_size) { -warning ("bad page read, with size of %d" , -kbuffer_subbuffer_size (kbuf)); +tracecmd_lib_warning ("bad page read, with size of %d" , +kbuffer_subbuffer_size (kbuf));return -1 ; } handle->cpu_data[cpu].timestamp = timestamp_calc(kbuffer_timestamp(kbuf),
@@ -2782,7 +2782,7 @@ static int handle_options(struct tracecmd_input *handle) buf + 8, 8);break ;default : -warning ("unknown option %d" , option); +tracecmd_lib_warning ("unknown option %d" , option);break ; }
@@ -2925,7 +2925,7 @@ static int read_cpu_data(struct tracecmd_input *handle)read8 (handle, &ignore);/* size */ if (ignore !=0 ) {if (!once) { -warning ("ignored CPU data not zero size" ); +tracecmd_lib_warning ("ignored CPU data not zero size" ); once++; } }
@@ -3057,7 +3057,7 @@ int tracecmd_init_data(struct tracecmd_input *handle)*/ if (read_and_parse_trace_clock (handle, pevent) <0 ) {char clock[] ="[local]" ; -warning ("File has trace_clock bug, using local clock" ); +tracecmd_lib_warning ("File has trace_clock bug, using local clock" );tracecmd_parse_trace_clock (handle, clock,8 ); } }
@@ -3455,7 +3455,7 @@ void tracecmd_close(struct tracecmd_input *handle)return ;if (handle->ref <=0 ) { -warning ("tracecmd: bad ref count on handle\n" ); +tracecmd_lib_warning ("tracecmd: bad ref count on handle\n" );return ; }
@@ -3472,10 +3472,10 @@ void tracecmd_close(struct tracecmd_input *handle)free_page_map (handle->cpu_data[cpu].page_map);if (handle->cpu_data[cpu].page_cnt) -warning ("%d pages still allocated on cpu %d%s" , - handle->cpu_data[cpu].page_cnt, - cpu,show_records (handle->cpu_data[cpu].pages, - handle->cpu_data[cpu].nr_pages)); +tracecmd_lib_warning ("%d pages still allocated on cpu %d%s" , + handle->cpu_data[cpu].page_cnt, + cpu,show_records (handle->cpu_data[cpu].pages, + handle->cpu_data[cpu].nr_pages));free (handle->cpu_data[cpu].pages); } }
@@ -3953,8 +3953,8 @@ tracecmd_buffer_instance_handle(struct tracecmd_input *handle, int indx) ret =lseek64 (handle->fd, buffer->offset,SEEK_SET );if (ret <0 ) { -warning ("could not seek to buffer %s offset %ld\n" , - buffer->name, buffer->offset); +tracecmd_lib_warning ("could not seek to buffer %s offset %ld\n" , + buffer->name, buffer->offset); goto error; }
@@ -3967,13 +3967,13 @@ tracecmd_buffer_instance_handle(struct tracecmd_input *handle, int indx)if (!ret) ret =read_cpu_data (new_handle);if (ret <0 ) { -warning ("failed to read sub buffer %s\n" , buffer->name); +tracecmd_lib_warning ("failed to read sub buffer %s\n" , buffer->name); goto error; } ret =lseek64 (handle->fd, offset,SEEK_SET );if (ret <0 ) { -warning ("could not seek to back to offset %ld\n" , offset); +tracecmd_lib_warning ("could not seek to back to offset %ld\n" , offset); goto error; }
lib/trace-cmd/trace-msg.c +7 -7
@@ -408,8 +408,8 @@ void tracecmd_msg_set_done(struct tracecmd_msg_handle *msg_handle)static void error_operation (struct tracecmd_msg *msg) { -warning ("Message: cmd=%d size=%d\n" , -ntohl (msg->hdr.cmd),ntohl (msg->hdr.size)); +tracecmd_lib_warning ("Message: cmd=%d size=%d\n" , +ntohl (msg->hdr.cmd),ntohl (msg->hdr.size)); }/*
@@ -439,7 +439,7 @@ static int tracecmd_msg_wait_for_msg(int fd, struct tracecmd_msg *msg) ret =tracecmd_msg_recv_wait (fd, msg);if (ret <0 ) {if (ret == -ETIMEDOUT ) -warning ("Connection timed out\n" ); +tracecmd_lib_warning ("Connection timed out\n" );return ret; }
@@ -590,7 +590,7 @@ int tracecmd_msg_initial_setting(struct tracecmd_msg_handle *msg_handle) ret =tracecmd_msg_recv_wait (msg_handle->fd, &msg);if (ret <0 ) {if (ret == -ETIMEDOUT ) -warning ("Connection timed out\n" ); +tracecmd_lib_warning ("Connection timed out\n" );return ret; }
@@ -754,9 +754,9 @@ int tracecmd_msg_read_data(struct tracecmd_msg_handle *msg_handle, int ofd) ret =tracecmd_msg_recv_wait (msg_handle->fd, &msg);if (ret <0 ) {if (ret == -ETIMEDOUT ) -warning ("Connection timed out\n" ); +tracecmd_lib_warning ("Connection timed out\n" );else -warning ("reading client" ); +tracecmd_lib_warning ("reading client" );return ret; }
@@ -779,7 +779,7 @@ int tracecmd_msg_read_data(struct tracecmd_msg_handle *msg_handle, int ofd)if (s <0 ) {if (errno ==EINTR )continue ; -warning ("writing to file" ); +tracecmd_lib_warning ("writing to file" ); ret = -errno; goto error; }
lib/trace-cmd/trace-output.c +47 -47
303 lines not on this page · raw message
lib/trace-cmd/trace-plugin.c +8 -7
@@ -10,6 +10,7 @@#include <libgen.h> #include "trace-cmd.h" #include "trace-local.h" +#include "trace-cmd-local.h" #define LOCAL_PLUGIN_DIR ".local/lib/trace-cmd/plugins/"
@@ -107,14 +108,14 @@ load_plugin(struct trace_plugin_context *trace, const char *path, ret =asprintf (&plugin,"%s/%s" , path, file);if (ret <0 ) { -warning ("could not allocate plugin memory\n" ); +tracecmd_lib_warning ("could not allocate plugin memory\n" );return ; } handle =dlopen (plugin,RTLD_NOW |RTLD_GLOBAL );if (!handle) { -warning ("could not load plugin '%s'\n%s\n" , - plugin,dlerror ()); +tracecmd_lib_warning ("could not load plugin '%s'\n%s\n" , + plugin,dlerror ()); goto out_free; }
@@ -124,14 +125,14 @@ load_plugin(struct trace_plugin_context *trace, const char *path, func =dlsym (handle,TRACECMD_PLUGIN_LOADER_NAME );if (!func) { -warning ("could not find func '%s' in plugin '%s'\n%s\n" , -TRACECMD_PLUGIN_LOADER_NAME , plugin,dlerror ()); +tracecmd_lib_warning ("could not find func '%s' in plugin '%s'\n%s\n" , +TRACECMD_PLUGIN_LOADER_NAME , plugin,dlerror ()); goto out_free; } list =malloc (sizeof (*list));if (!list) { -warning ("could not allocate plugin memory\n" ); +tracecmd_lib_warning ("could not allocate plugin memory\n" ); goto out_free; }
@@ -256,7 +257,7 @@ load_plugins_hook(struct trace_plugin_context *trace, const char *suffix, ret =asprintf (&path,"%s/%s" , home,LOCAL_PLUGIN_DIR );if (ret <0 ) { -warning ("could not allocate plugin memory\n" ); +tracecmd_lib_warning ("could not allocate plugin memory\n" );return ; }
lib/trace-cmd/trace-recorder.c +5 -4
@@ -14,6 +14,7 @@#include "tracefs.h" #include "trace-cmd-private.h" +#include "trace-cmd-local.h" #include "event-utils.h" /* F_GETPIPE_SZ was introduced in 2.6.35, older systems don't have it */
@@ -393,7 +394,7 @@ static long splice_data(struct tracecmd_recorder *recorder)if (errno ==EAGAIN || errno ==EINTR || errno ==ENOTCONN )return 0 ; -warning ("recorder error in splice input" ); +tracecmd_lib_warning ("recorder error in splice input" );return -1 ; }else if (read ==0 )return 0 ;
@@ -403,7 +404,7 @@ static long splice_data(struct tracecmd_recorder *recorder) read, recorder->fd_flags);if (ret <0 ) {if (errno !=EAGAIN && errno !=EINTR ) { -warning ("recorder error in splice output" ); +tracecmd_lib_warning ("recorder error in splice output" );return -1 ; }return total_read;
@@ -451,7 +452,7 @@ static long direct_splice_data(struct tracecmd_recorder *recorder)if (errno ==EAGAIN || errno ==EINTR || errno ==ENOTCONN )return 0 ; -warning ("recorder error in splice input" ); +tracecmd_lib_warning ("recorder error in splice input" );return -1 ; }
@@ -473,7 +474,7 @@ static long read_data(struct tracecmd_recorder *recorder)if (errno ==EAGAIN || errno ==EINTR || errno ==ENOTCONN )return 0 ; -warning ("recorder error in read input" ); +tracecmd_lib_warning ("recorder error in read input" );return -1 ; }
lib/trace-cmd/trace-timesync.c +2 -1
@@ -20,6 +20,7 @@#include <pthread.h> #include "trace-cmd-private.h" +#include "trace-cmd-local.h" #include "tracefs.h" #include "event-utils.h" #include "trace-tsync-local.h"
@@ -729,7 +730,7 @@ static int tsync_get_sample(struct tracecmd_time_sync *tsync, unsigned int cpu, ret = proto->clock_sync_calc (tsync, &offset, &scaling, ×tamp, cpu);if (ret) { -warning ("Failed to synchronize timestamps with guest" ); +tracecmd_lib_warning ("Failed to synchronize timestamps with guest" );return -1 ; }if (!offset || !timestamp || !scaling)
lib/trace-cmd/trace-util.c +11 -15
@@ -20,9 +20,10 @@#include <sys/stat.h> #include <sys/sysinfo.h> #include <time.h> +#include <traceevent/event-utils.h> #include "trace-cmd-private.h" -#include "event-utils.h" +#include "trace-cmd-local.h" #define LOCAL_PLUGIN_DIR ".trace-cmd/plugins"#define PROC_STACK_FILE "/proc/sys/kernel/stack_tracer_enabled"
@@ -182,7 +183,7 @@ void tracecmd_parse_ftrace_printk(struct tep_handle *pevent,while (line) { addr_str =strtok_r (line,":" , &fmt);if (!addr_str) { -warning ("printk format with empty entry" ); +tracecmd_lib_warning ("printk format with empty entry" );break ; } addr =strtoull (addr_str,NULL ,16 );
@@ -353,28 +354,23 @@ trace_load_plugins(struct tep_handle *tep, int flags)return list; } -static int __vlib_warning (const char *fmt,va_list ap) +void __weak tracecmd_lib_warning (const char *fmt, ...) { -int ret = errno; - -if (errno) -perror ("libtracecmd" ); - -fprintf (stderr," " ); -vfprintf (stderr, fmt, ap); - -fprintf (stderr,"\n" ); +va_list ap; -return ret; +va_start (ap, fmt); +tep_vwarning ("libtracecmd" , fmt, ap); +va_end (ap); } +void __weak tracecmd_lib_fatal (const char *fmt, ...) {int ret;va_list ap;va_start (ap, fmt); - ret =__vlib_warning (fmt, ap); + ret =tep_vwarning ("libtracecmd" , fmt, ap);va_end (ap);if (debug) {
@@ -524,7 +520,7 @@ int tracecmd_count_cpus(void)if (!once) { once++; -warning ("sysconf could not determine number of CPUS" ); +tracecmd_lib_warning ("sysconf could not determine number of CPUS" ); }/* Do the hack to figure out # of CPUS */
python/ctracecmd.i +1 -1
@@ -63,7 +63,7 @@ void warning(const char *fmt, ...) return; va_start(ap, fmt); - __vwarning(fmt, ap); + tep_vwarning("tracecmd", fmt, ap); va_end(ap); }
tracecmd/include/trace-local.h +2 -0
@@ -27,6 +27,8 @@ typedef unsigned long long u64;struct buffer_instance ; +void warning (const char *fmt, ...); +/* for local shared information with trace-cmd executable */ void usage (char **argv);
Signature · 1 line
2.30.2
Quoted 11 lines · On Thu, 8 Apr 2021 07:28:43 +0300 "Tzvetomir Stoyanov (VMware)"…
The
warning()function is used in a lot of places in the trace-cmd library, but there is no implementation. The function is implemented in the trace-cmd application. There is also a weak implementation in traceevent library, which is specific to that library. Implemented a newtracecmd_lib_warning(), specific to the trace-cmd library and replaced allwarning()calls with the new function in the library. The new function is implemented as weak, so it can be overridden by the application.
Thetracecm_lib_warning()usestep_vwarning()from libtraceevent for printing the warning, which is also a weak function and can be overridden by the application.
Hmm, this seems inconsistent with the other warnings. We have tep_warning(), tracefs_warning, and here it's tracecmd_lib_warning(). I wonder if it's better to drop the "_lib" part, and just call it, tracecmd_warning()?
And if we do, we should rename tracecmd_lib_fatal() to just tracecmd_fatal(). The "lib" just seems redundant.
-- Steve
Quoted 21 lines · Steven Rostedt
Quoted 11 lines · On Thu, 8 Apr 2021 07:28:43 +0300 "Tzvetomir Stoyanov (VMware)"…
The
warning()function is used in a lot of places in the trace-cmd library, but there is no implementation. The function is implemented in the trace-cmd application. There is also a weak implementation in traceevent library, which is specific to that library. Implemented a newtracecmd_lib_warning(), specific to the trace-cmd library and replaced allwarning()calls with the new function in the library. The new function is implemented as weak, so it can be overridden by the application.
Thetracecm_lib_warning()usestep_vwarning()from libtraceevent for printing the warning, which is also a weak function and can be overridden by the application.Hmm, this seems inconsistent with the other warnings. We have
tep_warning(),tracefs_warning, and here it'stracecmd_lib_warning(). I wonder if it's better to drop the "_lib" part, and just call it,tracecmd_warning()?
Quoted 4 lines
And if we do, we should rename
tracecmd_lib_fatal()to justtracecmd_fatal(). The "lib" just seems redundant.-- Steve
You are right, I'll rename both tracecmd_lib_warning() and tracecmd_lib_fatal(). I was confused, thinking that "tracecmd_" is the prefix for the application, but indeed it is used by the library.
Signature · 2 lines
Tzvetomir (Ceco) Stoyanov VMware Open Source Technology Center