Skip to contentSkip to conversation

Separate trace-cmd and libtracecmd code

PATCH Superseded Tzvetomir Stoyanov (VMware) 3 participants 12 messages Jul 11, 2019 – Aug 9, 2019

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Separate trace-cmd and libtracecmd code

libtracecmd is a library, containing functions that can be used without trace-cmd application. However, some of the functions declared as libtracecmd APIs in trace-cmd.h depend on trace-cmd context. That causes a problem when other application uses the library. The problem can be observed when running kerneshark and there is a python module, loaded by the python plugin - there is a bunch of warnings.
To resolve the problem, implementations of all trace-cmd independent functions are moved into libtracecmd. All libtracecmd functions, that depend on trace-cmd context are removed from the library and from trace-cmd.h file.

Tzvetomir Stoyanov (VMware) (7):
trace-cmd: Move trace-output.c into the library code
trace-cmd: Move trace-msg.c into the library.
trace-cmd: Move trace-cmd global variable "quiet" to libtracecmd
trace-cmd: Move trace-cmd global variable "debug" to libtracecmd
trace-cmd: Move plog() function to libtracecmd.
trace-cmd: Move trace-cmd APIs from trace-cmd.h to trace-local.h
trace-cmd: Move tracecmd_stack_tracer_status() function to libtracecmd

 include/trace-cmd/trace-cmd.h                 |  19 +-
 .../include => include/trace-cmd}/trace-msg.h |   3 -
 lib/trace-cmd/Makefile                        |  12 +-
 {tracecmd => lib/trace-cmd}/trace-msg.c       |   4 +-
 {tracecmd => lib/trace-cmd}/trace-output.c    |   4 +-
 lib/trace-cmd/trace-util.c                    | 162 ++++++++++++++++++
 tracecmd/Makefile                             |   2 -
 tracecmd/include/trace-cmd-local.h            |   2 -
 tracecmd/include/trace-local.h                |  14 +-
 tracecmd/trace-cmd.c                          |   3 -
 tracecmd/trace-list.c                         |   2 +-
 tracecmd/trace-listen.c                       |  77 ++-------
 tracecmd/trace-read.c                         |   8 +-
 tracecmd/trace-record.c                       |   8 +-
 tracecmd/trace-stack.c                        |  56 +-----
 15 files changed, 221 insertions(+), 155 deletions(-)
 rename {tracecmd/include => include/trace-cmd}/trace-msg.h (79%)
 rename {tracecmd => lib/trace-cmd}/trace-msg.c (99%)
 rename {tracecmd => lib/trace-cmd}/trace-output.c (99%)
Signature · 1 line
2.21.0
#1 lore raw

PATCH 1/7 trace-cmd: Move trace-output.c into the library code

Functions, implemented in trace-output.c file, do not depend on trace-cmd application context and can be used standalone. The file is moved from trace-cmd to libtracecmd. It also fixes a warning while loading python modules from kernelshark:
ImportError: ctracecmd.so: undefined symbol: tracecmd_append_cpu_data

---
 lib/trace-cmd/Makefile                     | 11 ++++++++++-
 {tracecmd => lib/trace-cmd}/trace-output.c |  0
 tracecmd/Makefile                          |  1 -
 3 files changed, 10 insertions(+), 2 deletions(-)
 rename {tracecmd => lib/trace-cmd}/trace-output.c (100%)
lib/trace-cmd/Makefile +10 -1
index 44c1332..9086eb6 100644
@@ -4,12 +4,15 @@ include $(src)/scripts/utils.mk
 
 bdir:=$(obj)/lib/trace-cmd
 
-DEFAULT_TARGET = $(bdir)/libtracecmd.a
+TC_VERSION := $(bdir)/include/tc_version.h
+
+DEFAULT_TARGET = $(bdir)/libtracecmd.a $(TC_VERSION)
 
 OBJS =
 OBJS += trace-hash.o
 OBJS += trace-hooks.o
 OBJS += trace-input.o
+OBJS += trace-output.o
 OBJS += trace-recorder.o
 OBJS += trace-util.o
 OBJS += trace-filter-hash.o
@@ -29,6 +32,9 @@ $(bdir):
 $(OBJS): | $(bdir)
 $(DEPS): | $(bdir)
 
+$(TC_VERSION): force | $(bdir)/include
+	$(Q)$(call update_version.h)
+
 $(bdir)/libtracecmd.a: $(OBJS)
 	$(Q)$(call do_build_static_lib)
 
@@ -43,6 +49,8 @@ $(bdir)/trace-util.o: $(obj)/plugins/trace_plugin_dir
 $(DEPS): $(bdir)/.%.d: %.c
 	$(Q)$(CC) -M $(CPPFLAGS) $(CFLAGS) $< > $@
 
+$(DEPS): $(TC_VERSION)
+
 $(OBJS): $(bdir)/%.o : $(bdir)/.%.d
 
 dep_includes := $(wildcard $(DEPS))
@@ -54,4 +62,5 @@ endif
 clean:
 	$(RM) $(bdir)/*.a $(bdir)/*.so $(bdir)/*.o $(bdir)/.*.d
 
+force:
 .PHONY: clean
lib/trace-cmd/trace-output.c renamed from tracecmd/trace-output.c +0 -0
similarity index 100%
rename from tracecmd/trace-output.c
rename to lib/trace-cmd/trace-output.c
tracecmd/Makefile +0 -1
index bcd437a..6968f83 100644
@@ -29,7 +29,6 @@ TRACE_CMD_OBJS += trace-restore.o
 TRACE_CMD_OBJS += trace-check-events.o
 TRACE_CMD_OBJS += trace-show.o
 TRACE_CMD_OBJS += trace-list.o
-TRACE_CMD_OBJS += trace-output.o
 TRACE_CMD_OBJS += trace-usage.o
 TRACE_CMD_OBJS += trace-msg.o
 
Signature · 1 line
2.21.0
#2 lore raw

Re: [PATCH 1/7] trace-cmd: Move trace-output.c into the library code

Quoted 36 lines · On Thu, 11 Jul 2019 16:03:01 +0300 "Tzvetomir Stoyanov (VMware)"…

Functions, implemented in trace-output.c file, do not depend on trace-cmd application context and can be used standalone. The file is moved from trace-cmd to libtracecmd. It also fixes a warning while loading python modules from kernelshark:
ImportError: ctracecmd.so: undefined symbol: tracecmd_append_cpu_data

---
 lib/trace-cmd/Makefile                     | 11 ++++++++++-
 {tracecmd => lib/trace-cmd}/trace-output.c |  0
 tracecmd/Makefile                          |  1 -
 3 files changed, 10 insertions(+), 2 deletions(-)
 rename {tracecmd => lib/trace-cmd}/trace-output.c (100%)
lib/trace-cmd/Makefile +4 -1
index 44c1332..9086eb6 100644
@@ -4,12 +4,15 @@ include $(src)/scripts/utils.mk
 
 bdir:=$(obj)/lib/trace-cmd
 
-DEFAULT_TARGET = $(bdir)/libtracecmd.a
+TC_VERSION := $(bdir)/include/tc_version.h
+
+DEFAULT_TARGET = $(bdir)/libtracecmd.a $(TC_VERSION)
 
 OBJS =
 OBJS += trace-hash.o
 OBJS += trace-hooks.o
 OBJS += trace-input.o
+OBJS += trace-output.o
 OBJS += trace-recorder.o
 OBJS += trace-util.o
 OBJS += trace-filter-hash.o

lib/trace-cmd/Makefile $(bdir): · line 37

Quoted 6 lines
lib/trace-cmd/Makefile +3 -0
@@ -29,6 +32,9 @@ $(bdir):
 $(OBJS): | $(bdir)
 $(DEPS): | $(bdir)
 
+$(TC_VERSION): force | $(bdir)/include
+	$(Q)$(call update_version.h)
+

Hi Ceco,

Why the addition of TC_VERISON here? It's not stated in the change log to why this was copied over. I ever removed all references from TC_VERSION and it still builds fine.

-- Steve

Quoted 34 lines
lib/trace-cmd/Makefile +3 -0
 $(bdir)/libtracecmd.a: $(OBJS)
 	$(Q)$(call do_build_static_lib)
 
@@ -43,6 +49,8 @@ $(bdir)/trace-util.o: $(obj)/plugins/trace_plugin_dir
 $(DEPS): $(bdir)/.%.d: %.c
 	$(Q)$(CC) -M $(CPPFLAGS) $(CFLAGS) $< > $@
 
+$(DEPS): $(TC_VERSION)
+
 $(OBJS): $(bdir)/%.o : $(bdir)/.%.d
 
 dep_includes := $(wildcard $(DEPS))
@@ -54,4 +62,5 @@ endif
 clean:
 	$(RM) $(bdir)/*.a $(bdir)/*.so $(bdir)/*.o $(bdir)/.*.d
 
+force:
 .PHONY: clean
lib/trace-cmd/trace-output.c renamed from tracecmd/trace-output.c +0 -0
similarity index 100%
rename from tracecmd/trace-output.c
rename to lib/trace-cmd/trace-output.c
tracecmd/Makefile +0 -1
index bcd437a..6968f83 100644
@@ -29,7 +29,6 @@ TRACE_CMD_OBJS += trace-restore.o
 TRACE_CMD_OBJS += trace-check-events.o
 TRACE_CMD_OBJS += trace-show.o
 TRACE_CMD_OBJS += trace-list.o
-TRACE_CMD_OBJS += trace-output.o
 TRACE_CMD_OBJS += trace-usage.o
 TRACE_CMD_OBJS += trace-msg.o
 
#3 lore raw

Re: [PATCH 1/7] trace-cmd: Move trace-output.c into the library code

Quoted 49 lines · Steven Rostedt
Quoted 36 lines · On Thu, 11 Jul 2019 16:03:01 +0300 "Tzvetomir Stoyanov (VMware)"…

Functions, implemented in trace-output.c file, do not depend on trace-cmd application context and can be used standalone. The file is moved from trace-cmd to libtracecmd. It also fixes a warning while loading python modules from kernelshark:
ImportError: ctracecmd.so: undefined symbol: tracecmd_append_cpu_data

---
 lib/trace-cmd/Makefile                     | 11 ++++++++++-
 {tracecmd => lib/trace-cmd}/trace-output.c |  0
 tracecmd/Makefile                          |  1 -
 3 files changed, 10 insertions(+), 2 deletions(-)
 rename {tracecmd => lib/trace-cmd}/trace-output.c (100%)
lib/trace-cmd/Makefile +4 -1
index 44c1332..9086eb6 100644
@@ -4,12 +4,15 @@ include $(src)/scripts/utils.mk
 
 bdir:=$(obj)/lib/trace-cmd
 
-DEFAULT_TARGET = $(bdir)/libtracecmd.a
+TC_VERSION := $(bdir)/include/tc_version.h
+
+DEFAULT_TARGET = $(bdir)/libtracecmd.a $(TC_VERSION)
 
 OBJS =
 OBJS += trace-hash.o
 OBJS += trace-hooks.o
 OBJS += trace-input.o
+OBJS += trace-output.o
 OBJS += trace-recorder.o
 OBJS += trace-util.o
 OBJS += trace-filter-hash.o

lib/trace-cmd/Makefile $(bdir): · line 37

Quoted 6 lines
lib/trace-cmd/Makefile +3 -0
@@ -29,6 +32,9 @@ $(bdir):
 $(OBJS): | $(bdir)
 $(DEPS): | $(bdir)
 
+$(TC_VERSION): force | $(bdir)/include
+     $(Q)$(call update_version.h)
+

Hi Ceco,

Quoted 6 lines

Why the addition of TC_VERISON here? It's not stated in the change log to why this was copied over. I ever removed all references from TC_VERSION and it still builds fine.

-- Steve

There is a dependency: trace-output.c includes version.h, which includes tc_version.h.
When trace-output.c is moved to the lib/trace-cmd directory, it includes tc_version.h from
lib/trace-cmd/include. It makes sense, otherwise the libtracecmd will depend on the trace-cmd
application. That's why I duplicated that TC_VERISON logic in lib/trace-cmd/Makefile. It
forces lib/trace-cmd/include/tc_version.h to be generated on every libtracecmd build.
Now we have two auto generated files:
lib/trace-cmd/include/tc_version.h tracecmd/include/tc_version.h

May be we should move tc_version.h to be generated in include/, so we can use the same file from
both the application and the library ?

Quoted 36 lines
Quoted 35 lines
lib/trace-cmd/Makefile +3 -0
 $(bdir)/libtracecmd.a: $(OBJS)
      $(Q)$(call do_build_static_lib)
 
@@ -43,6 +49,8 @@ $(bdir)/trace-util.o: $(obj)/plugins/trace_plugin_dir
 $(DEPS): $(bdir)/.%.d: %.c
      $(Q)$(CC) -M $(CPPFLAGS) $(CFLAGS) $< > $@
 
+$(DEPS): $(TC_VERSION)
+
 $(OBJS): $(bdir)/%.o : $(bdir)/.%.d
 
 dep_includes := $(wildcard $(DEPS))
@@ -54,4 +62,5 @@ endif
 clean:
      $(RM) $(bdir)/*.a $(bdir)/*.so $(bdir)/*.o $(bdir)/.*.d
 
+force:
 .PHONY: clean
lib/trace-cmd/trace-output.c renamed from tracecmd/trace-output.c +0 -0
similarity index 100%
rename from tracecmd/trace-output.c
rename to lib/trace-cmd/trace-output.c
tracecmd/Makefile +0 -1
index bcd437a..6968f83 100644
@@ -29,7 +29,6 @@ TRACE_CMD_OBJS += trace-restore.o
 TRACE_CMD_OBJS += trace-check-events.o
 TRACE_CMD_OBJS += trace-show.o
 TRACE_CMD_OBJS += trace-list.o
-TRACE_CMD_OBJS += trace-output.o
 TRACE_CMD_OBJS += trace-usage.o
 TRACE_CMD_OBJS += trace-msg.o
 
Signature · 2 lines
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
#4 lore raw

Re: [PATCH 1/7] trace-cmd: Move trace-output.c into the library code

Quoted 12 lines · On Fri, 9 Aug 2019 13:22:29 +0000 Tzvetomir Stoyanov <[email protected]>…
Quoted 4 lines

lib/trace-cmd/Makefile $(bdir): · line 37

Quoted 3 lines

+$(TC_VERSION): force | $(bdir)/include

lib/trace-cmd/Makefile +2 -0
+     $(Q)$(call update_version.h)
+  
Quoted 8 lines

Hi Ceco,

Why the addition of TC_VERISON here? It's not stated in the change log to why this was copied over. I ever removed all references from TC_VERSION and it still builds fine.

-- Steve

Quoted 10 lines

There is a dependency: trace-output.c includes version.h, which includes tc_version.h.
When trace-output.c is moved to the lib/trace-cmd directory, it includes tc_version.h from
lib/trace-cmd/include. It makes sense, otherwise the libtracecmd will depend on the trace-cmd
application. That's why I duplicated that TC_VERISON logic in lib/trace-cmd/Makefile. It
forces lib/trace-cmd/include/tc_version.h to be generated on every libtracecmd build.

Yeah, we can't have that.

Quoted 7 lines

Now we have two auto generated files:
lib/trace-cmd/include/tc_version.h
tracecmd/include/tc_version.h

May be we should move tc_version.h to be generated in include/, so we can use the same file from
both the application and the library ?

I looked at why we have version.h there, and it's to get FILE_VERSION_STRING, which is only used in trace-output.c.

And this is created via the Makefile. Hmm, we may need to move all the file version logic into lib/trace-event as the only files that should ever care about this is trace-output.c and trace-input.c, which are both in the lib/trace-event (not something trace-cmd should worry about).

-- Steve

Quoted 39 lines
Quoted 37 lines
Quoted 36 lines

$(bdir)/libtracecmd.a: $(OBJS)

      $(Q)$(call do_build_static_lib)
lib/trace-cmd/Makefile +3 -0
@@ -43,6 +49,8 @@ $(bdir)/trace-util.o: $(obj)/plugins/trace_plugin_dir
 $(DEPS): $(bdir)/.%.d: %.c
      $(Q)$(CC) -M $(CPPFLAGS) $(CFLAGS) $< > $@
 
+$(DEPS): $(TC_VERSION)
+
 $(OBJS): $(bdir)/%.o : $(bdir)/.%.d
 
 dep_includes := $(wildcard $(DEPS))
@@ -54,4 +62,5 @@ endif
 clean:
      $(RM) $(bdir)/*.a $(bdir)/*.so $(bdir)/*.o $(bdir)/.*.d
 
+force:
 .PHONY: clean
lib/trace-cmd/trace-output.c renamed from tracecmd/trace-output.c +0 -0
similarity index 100%
rename from tracecmd/trace-output.c
rename to lib/trace-cmd/trace-output.c
tracecmd/Makefile +0 -1
index bcd437a..6968f83 100644
@@ -29,7 +29,6 @@ TRACE_CMD_OBJS += trace-restore.o
 TRACE_CMD_OBJS += trace-check-events.o
 TRACE_CMD_OBJS += trace-show.o
 TRACE_CMD_OBJS += trace-list.o
-TRACE_CMD_OBJS += trace-output.o
 TRACE_CMD_OBJS += trace-usage.o
 TRACE_CMD_OBJS += trace-msg.o
 
#5 lore raw

Re: [PATCH 1/7] trace-cmd: Move trace-output.c into the library code

Quoted 2 lines · On Fri, 9 Aug 2019 09:59:07 -0400 Steven Rostedt <[email protected]> wrote:

And this is created via the Makefile. Hmm, we may need to move all the file version logic into lib/trace-event as the only files that should

sorry, that should have said lib/trace-cmd/

-- Steve

Quoted 4 lines

ever care about this is trace-output.c and trace-input.c, which are both in the lib/trace-event (not something trace-cmd should worry about).

#6 lore raw

PATCH 2/7 trace-cmd: Move trace-msg.c into the library.

In reply to Tzvetomir Stoyanov (VMware)

Functions, implemented in trace-msg.c file, do not depend on trace-cmd application context and can be used standalone. The file is moved from trace-cmd to libtracecmd.

---
 {tracecmd/include => include/trace-cmd}/trace-msg.h | 0
 lib/trace-cmd/Makefile                              | 1 +
 {tracecmd => lib/trace-cmd}/trace-msg.c             | 0
 tracecmd/Makefile                                   | 1 -
 4 files changed, 1 insertion(+), 1 deletion(-)
 rename {tracecmd/include => include/trace-cmd}/trace-msg.h (100%)
 rename {tracecmd => lib/trace-cmd}/trace-msg.c (100%)
include/trace-cmd/trace-msg.h renamed from tracecmd/include/trace-msg.h +0 -0
similarity index 100%
rename from tracecmd/include/trace-msg.h
rename to include/trace-cmd/trace-msg.h
lib/trace-cmd/Makefile +1 -0
index 9086eb6..5f7b111 100644
@@ -16,6 +16,7 @@ OBJS += trace-output.o
 OBJS += trace-recorder.o
 OBJS += trace-util.o
 OBJS += trace-filter-hash.o
+OBJS += trace-msg.o
 
 # Additional util objects
 OBJS += trace-blk-hack.o
lib/trace-cmd/trace-msg.c renamed from tracecmd/trace-msg.c +0 -0
similarity index 100%
rename from tracecmd/trace-msg.c
rename to lib/trace-cmd/trace-msg.c
tracecmd/Makefile +0 -1
index 6968f83..d491aae 100644
@@ -30,7 +30,6 @@ TRACE_CMD_OBJS += trace-check-events.o
 TRACE_CMD_OBJS += trace-show.o
 TRACE_CMD_OBJS += trace-list.o
 TRACE_CMD_OBJS += trace-usage.o
-TRACE_CMD_OBJS += trace-msg.o
 
 ALL_OBJS := $(TRACE_CMD_OBJS:%.o=$(bdir)/%.o)
 
Signature · 1 line
2.21.0
#7 lore raw

PATCH 3/7 trace-cmd: Move trace-cmd global variable "quiet" to libtracecmd

In reply to Tzvetomir Stoyanov (VMware)

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);

---
 include/trace-cmd/trace-cmd.h      |  3 +++
 lib/trace-cmd/trace-output.c       |  4 ++--
 lib/trace-cmd/trace-util.c         | 21 +++++++++++++++++++++
 tracecmd/include/trace-cmd-local.h |  2 --
 tracecmd/include/trace-local.h     |  1 -
 tracecmd/trace-cmd.c               |  1 -
 tracecmd/trace-record.c            |  6 +++---
 7 files changed, 29 insertions(+), 9 deletions(-)
include/trace-cmd/trace-cmd.h +3 -0
index 6f62ab9..8c8f87c 100644
@@ -49,6 +49,9 @@ enum {
 void tracecmd_record_ref(struct tep_record *record);
 void free_record(struct tep_record *record);
 
+void tracecmd_set_quiet(int quiet);
+int tracecmd_get_quiet(void);
+
 struct tracecmd_input;
 struct tracecmd_output;
 struct tracecmd_recorder;
lib/trace-cmd/trace-output.c +2 -2
index 33d6ce3..6ca7110 100644
@@ -1158,7 +1158,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())
 			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);
@@ -1173,7 +1173,7 @@ int tracecmd_write_cpu_data(struct tracecmd_output *handle,
 			    check_size, sizes[i]);
 			goto out_free;
 		}
-		if (!quiet)
+		if (!tracecmd_get_quiet())
 			fprintf(stderr, "    %llu bytes in size\n",
 				(unsigned long long)check_size);
 	}
lib/trace-cmd/trace-util.c +21 -0
index 7c74bae..26b9a18 100644
@@ -28,6 +28,7 @@
 
 int tracecmd_disable_sys_plugins;
 int tracecmd_disable_plugins;
+static int tracecmd_quiet;
 
 static struct registered_plugin_options {
 	struct registered_plugin_options	*next;
@@ -96,6 +97,26 @@ char **trace_util_list_plugin_options(void)
 	return list;
 }
 
+/**
+ * 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(int quiet)
+{
+	tracecmd_quiet = quiet;
+}
+
+/**
+ * tracecmd_get_quiet - Get if to print output to the screen
+ * Returns non zero, if no output to the screen should be printed
+ *
+ */
+int tracecmd_get_quiet(void)
+{
+	return tracecmd_quiet;
+}
+
 void trace_util_free_plugin_options_list(char **list)
 {
 	tracecmd_free_list(list);
tracecmd/include/trace-cmd-local.h +0 -2
index fa96d4f..16db7a3 100644
@@ -11,8 +11,6 @@
 #include "trace-cmd.h"
 #include "event-utils.h"
 
-extern int quiet;
-
 static ssize_t __do_write(int fd, const void *data, size_t size)
 {
 	ssize_t tot = 0;
tracecmd/include/trace-local.h +0 -1
index 1cad3cc..8fbafa6 100644
@@ -13,7 +13,6 @@
 #include "event-utils.h"
 
 extern int debug;
-extern int quiet;
 
 /* fix stupid glib guint64 typecasts and printf formats */
 typedef unsigned long long u64;
tracecmd/trace-cmd.c +0 -1
index 797b303..5283ba7 100644
@@ -17,7 +17,6 @@ int silence_warnings;
 int show_status;
 
 int debug;
-int quiet;
 
 void warning(const char *fmt, ...)
 {
tracecmd/trace-record.c +3 -3
index 5dc6f17..e647117 100644
@@ -3163,7 +3163,7 @@ static void print_stat(struct buffer_instance *instance)
 {
 	int cpu;
 
-	if (quiet)
+	if (tracecmd_get_quiet())
 		return;
 
 	if (!is_top_instance(instance))
@@ -3979,7 +3979,7 @@ static void check_plugin(const char *plugin)
 	}
 	die ("Plugin '%s' does not exist", plugin);
  out:
-	if (!quiet)
+	if (!tracecmd_get_quiet())
 		fprintf(stderr, "  plugin '%s'\n", plugin);
 	free(buf);
 }
@@ -4913,7 +4913,7 @@ static void parse_record_options(int argc,
 			break;
 		case OPT_quiet:
 		case 'q':
-			quiet = 1;
+			tracecmd_set_quiet(1);
 			break;
 		default:
 			usage(argv);
Signature · 1 line
2.21.0
#8 lore raw

PATCH 4/7 trace-cmd: Move trace-cmd global variable "debug" to libtracecmd

In reply to Tzvetomir Stoyanov (VMware)

A trace-cmd global variable "debug" is used from libtracecmd and should be defined there. A new library APIs are implemented to access it:
void tracecmd_set_debug(bool debug);
bool tracecmd_get_debug(void);

---
 include/trace-cmd/trace-cmd.h  |  3 +++
 lib/trace-cmd/trace-msg.c      |  4 ++--
 lib/trace-cmd/trace-util.c     | 21 +++++++++++++++++++++
 tracecmd/include/trace-local.h |  2 --
 tracecmd/trace-cmd.c           |  2 --
 tracecmd/trace-list.c          |  2 +-
 tracecmd/trace-listen.c        |  8 ++++----
 tracecmd/trace-read.c          |  8 ++++----
 tracecmd/trace-record.c        |  2 +-
 9 files changed, 36 insertions(+), 16 deletions(-)
include/trace-cmd/trace-cmd.h +3 -0
index 8c8f87c..2f524b6 100644
@@ -51,6 +51,9 @@ void free_record(struct tep_record *record);
 
 void tracecmd_set_quiet(int quiet);
 int tracecmd_get_quiet(void);
+void tracecmd_set_debug(bool debug);
+bool tracecmd_get_debug(void);
+
 
 struct tracecmd_input;
 struct tracecmd_output;
lib/trace-cmd/trace-msg.c +2 -2
index e2dd188..92562c7 100644
@@ -32,7 +32,7 @@ static inline void dprint(const char *fmt, ...)
 {
 	va_list ap;
 
-	if (!debug)
+	if (!tracecmd_get_debug())
 		return;
 
 	va_start(ap, fmt);
@@ -351,7 +351,7 @@ static int tracecmd_msg_recv_wait(int fd, struct tracecmd_msg *msg)
 
 	pfd.fd = fd;
 	pfd.events = POLLIN;
-	ret = poll(&pfd, 1, debug ? -1 : msg_wait_to);
+	ret = poll(&pfd, 1, tracecmd_get_debug() ? -1 : msg_wait_to);
 	if (ret < 0)
 		return -errno;
 	else if (ret == 0)
lib/trace-cmd/trace-util.c +21 -0
index 26b9a18..b5ce84f 100644
@@ -29,6 +29,7 @@
 int tracecmd_disable_sys_plugins;
 int tracecmd_disable_plugins;
 static int tracecmd_quiet;
+static bool tracecmd_debug;
 
 static struct registered_plugin_options {
 	struct registered_plugin_options	*next;
@@ -117,6 +118,26 @@ int tracecmd_get_quiet(void)
 	return tracecmd_quiet;
 }
 
+/**
+ * tracecmd_set_quiet - Set if to print output to the screen
+ * @quiet: If non zero, print no output to the screen
+ *
+ */
+void tracecmd_set_debug(bool debug)
+{
+	tracecmd_debug = debug;
+}
+
+/**
+ * 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_debug(void)
+{
+	return tracecmd_debug;
+}
+
 void trace_util_free_plugin_options_list(char **list)
 {
 	tracecmd_free_list(list);
tracecmd/include/trace-local.h +0 -2
index 8fbafa6..1dc2383 100644
@@ -12,8 +12,6 @@
 #include "trace-cmd.h"
 #include "event-utils.h"
 
-extern int debug;
-
 /* fix stupid glib guint64 typecasts and printf formats */
 typedef unsigned long long u64;
 
tracecmd/trace-cmd.c +0 -2
index 5283ba7..30691b6 100644
@@ -16,8 +16,6 @@
 int silence_warnings;
 int show_status;
 
-int debug;
-
 void warning(const char *fmt, ...)
 {
 	va_list ap;
tracecmd/trace-list.c +1 -1
index 00c6073..832540e 100644
@@ -427,7 +427,7 @@ void trace_list(int argc, char **argv)
 				break;
 			case '-':
 				if (strcmp(argv[i], "--debug") == 0) {
-					debug = true;
+					tracecmd_set_debug(true);
 					break;
 				}
 				fprintf(stderr, "list: invalid option -- '%s'\n",
tracecmd/trace-listen.c +4 -4
index 9132582..9dcb833 100644
@@ -717,7 +717,7 @@ static int do_fork(int cfd)
 	pid_t pid;
 
 	/* in debug mode, we do not fork off children */
-	if (debug)
+	if (tracecmd_get_debug())
 		return 0;
 
 	pid = fork();
@@ -769,7 +769,7 @@ static int do_connection(int cfd, struct sockaddr_storage *peer_addr,
 
 	tracecmd_msg_handle_close(msg_handle);
 
-	if (!debug)
+	if (!tracecmd_get_debug())
 		exit(0);
 
 	return 0;
@@ -910,7 +910,7 @@ static void do_listen(char *port)
 	struct addrinfo *result, *rp;
 	int sfd, s;
 
-	if (!debug)
+	if (!tracecmd_get_debug())
 		signal_setup(SIGCHLD, sigstub);
 
 	make_pid_file();
@@ -1009,7 +1009,7 @@ void trace_listen(int argc, char **argv)
 			daemon = 1;
 			break;
 		case OPT_debug:
-			debug = 1;
+			tracecmd_set_debug(true);
 			break;
 		default:
 			usage(argv);
tracecmd/trace-read.c +4 -4
index d22c723..12b8b3d 100644
@@ -782,10 +782,10 @@ void trace_show_data(struct tracecmd_input *handle, struct tep_record *record)
 				 cpu, record->missed_events);
 	else if (record->missed_events < 0)
 		trace_seq_printf(&s, "CPU:%d [EVENTS DROPPED]\n", cpu);
-	if (buffer_breaks || debug) {
+	if (buffer_breaks || tracecmd_get_debug()) {
 		if (tracecmd_record_at_buffer_start(handle, record)) {
 			trace_seq_printf(&s, "CPU:%d [SUBBUFFER START]", cpu);
-			if (debug)
+			if (tracecmd_get_debug())
 				trace_seq_printf(&s, " [%lld:0x%llx]",
 						 tracecmd_page_ts(handle, record),
 						 record->offset & ~(page_size - 1));
@@ -816,7 +816,7 @@ void trace_show_data(struct tracecmd_input *handle, struct tep_record *record)
 		tep_print_event(pevent, &s, record, use_trace_clock);
 	if (s.len && *(s.buffer + s.len - 1) == '\n')
 		s.len--;
-	if (debug) {
+	if (tracecmd_get_debug()) {
 		struct kbuffer *kbuf;
 		struct kbuffer_raw_info info;
 		void *page;
@@ -1616,7 +1616,7 @@ void trace_report (int argc, char **argv)
 			break;
 		case OPT_debug:
 			buffer_breaks = 1;
-			debug = 1;
+			tracecmd_set_debug(true);
 			break;
 		case OPT_profile:
 			profile = 1;
tracecmd/trace-record.c +1 -1
index e647117..8e2c83a 100644
@@ -4902,7 +4902,7 @@ static void parse_record_options(int argc,
 			no_filter = true;
 			break;
 		case OPT_debug:
-			debug = 1;
+			tracecmd_set_debug(true);
 			break;
 		case OPT_module:
 			if (ctx->instance->filter_mod)
Signature · 1 line
2.21.0
#9 lore raw

PATCH 5/7 trace-cmd: Move plog() function to libtracecmd.

In reply to Tzvetomir Stoyanov (VMware)

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 2 additional APIs are implemented:
int trace_set_log_file(char *logfile); - use it to set
the log file.
void 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 plog() library function.

---
 include/trace-cmd/trace-cmd.h |  4 ++
 include/trace-cmd/trace-msg.h |  3 --
 lib/trace-cmd/trace-util.c    | 71 +++++++++++++++++++++++++++++++++++
 tracecmd/trace-listen.c       | 69 ++++------------------------------
 4 files changed, 83 insertions(+), 64 deletions(-)
include/trace-cmd/trace-cmd.h +4 -0
index 2f524b6..5cfb028 100644
@@ -388,6 +388,10 @@ struct hook_list {
 struct hook_list *tracecmd_create_event_hook(const char *arg);
 void tracecmd_free_hooks(struct hook_list *hooks);
 
+void plog(const char *fmt, ...);
+void plog_error(const char *fmt, ...);
+int trace_set_log_file(char *logfile);
+
 /* --- Hack! --- */
 int tracecmd_blk_hack(struct tracecmd_input *handle);
 
include/trace-cmd/trace-msg.h +0 -3
index b7fe10b..aab8a69 100644
@@ -12,7 +12,4 @@
 
 extern unsigned int page_size;
 
-void plog(const char *fmt, ...);
-void pdie(const char *fmt, ...);
-
 #endif /* _TRACE_MSG_H_ */
lib/trace-cmd/trace-util.c +71 -0

80 lines not on this page · raw message

tracecmd/trace-listen.c +8 -61

99 lines not on this page · raw message

Signature · 1 line
2.21.0
#10 lore raw

PATCH 6/7 trace-cmd: Move trace-cmd APIs from trace-cmd.h to trace-local.h

In reply to Tzvetomir Stoyanov (VMware)

These APIs depend on trace-cmd context and cannot be used standalone:
tracecmd_create_top_instance() tracecmd_remove_instances()
tracecmd_filter_pid()
tracecmd_add_event()
tracecmd_enable_events() tracecmd_disable_all_tracing() tracecmd_disable_tracing()
tracecmd_enable_tracing()
tracecmd_stat_cpu()
They are implemented in trace-cmd application, but declared as APIs in trace-cmd.h. The declarations are moved from trace-cmd.h to trace-local.h, local header file visible only to trace-cmd application.

---
 include/trace-cmd/trace-cmd.h  |  9 ---------
 tracecmd/include/trace-local.h | 11 +++++++++++
 2 files changed, 11 insertions(+), 9 deletions(-)
include/trace-cmd/trace-cmd.h +0 -9
index 5cfb028..409a6ab 100644
@@ -130,8 +130,6 @@ int tracecmd_buffer_instances(struct tracecmd_input *handle);
 const char *tracecmd_buffer_instance_name(struct tracecmd_input *handle, int indx);
 struct tracecmd_input *tracecmd_buffer_instance_handle(struct tracecmd_input *handle, int indx);
 int tracecmd_is_buffer_instance(struct tracecmd_input *handle);
-void tracecmd_create_top_instance(char *name);
-void tracecmd_remove_instances(void);
 
 void tracecmd_set_ts_offset(struct tracecmd_input *handle, unsigned long long offset);
 void tracecmd_set_ts2secs(struct tracecmd_input *handle, unsigned long long hz);
@@ -294,14 +292,7 @@ struct tracecmd_recorder *tracecmd_create_buffer_recorder_maxkb(const char *file
 
 int tracecmd_start_recording(struct tracecmd_recorder *recorder, unsigned long sleep);
 void tracecmd_stop_recording(struct tracecmd_recorder *recorder);
-void tracecmd_stat_cpu(struct trace_seq *s, int cpu);
 long tracecmd_flush_recording(struct tracecmd_recorder *recorder);
-void tracecmd_filter_pid(int pid, int exclude);
-int tracecmd_add_event(const char *event_str, int stack);
-void tracecmd_enable_events(void);
-void tracecmd_disable_all_tracing(int disable_tracer);
-void tracecmd_disable_tracing(void);
-void tracecmd_enable_tracing(void);
 
 enum tracecmd_msg_flags {
 	TRACECMD_MSG_FL_USE_TCP		= 1 << 0,
tracecmd/include/trace-local.h +11 -0
index 1dc2383..38dfdee 100644
@@ -206,6 +206,17 @@ void show_instance_file(struct buffer_instance *instance, const char *name);
 
 int count_cpus(void);
 
+/* moved from trace-cmd.h */
+void tracecmd_create_top_instance(char *name);
+void tracecmd_remove_instances(void);
+void tracecmd_filter_pid(int pid, int exclude);
+int tracecmd_add_event(const char *event_str, int stack);
+void tracecmd_enable_events(void);
+void tracecmd_disable_all_tracing(int disable_tracer);
+void tracecmd_disable_tracing(void);
+void tracecmd_enable_tracing(void);
+void tracecmd_stat_cpu(struct trace_seq *s, int cpu);
+
 /* No longer in event-utils.h */
 void __noreturn die(const char *fmt, ...); /* Can be overriden */
 void *malloc_or_die(unsigned int size); /* Can be overridden */
Signature · 1 line
2.21.0
#11 lore raw

PATCH 7/7 trace-cmd: Move tracecmd_stack_tracer_status() function to libtracecmd

In reply to Tzvetomir Stoyanov (VMware)

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.

---
 lib/trace-cmd/trace-util.c | 49 +++++++++++++++++++++++++++++++++
 tracecmd/trace-stack.c     | 56 ++------------------------------------
 2 files changed, 51 insertions(+), 54 deletions(-)
lib/trace-cmd/trace-util.c +49 -0
index 8c1a0a0..d16a018 100644
@@ -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;
@@ -1787,3 +1788,51 @@ int trace_set_log_file(char *logfile)
 	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 enabled) 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 */
+}
tracecmd/trace-stack.c +2 -54

74 lines not on this page · raw message

Signature · 1 line
2.21.0