Separate trace-cmd and libtracecmd code
Subsystems (2)
v2 cover Separate trace-cmd and libtracecmd code
libtracecmd is a library, containing functions that can be used without the 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.
[
v2 changes:
- Added a new patch:
Move trace-cmd-local.h from the application to the library.
- Remove trace-output.c dependency of version.h. Moved FILE_VERSION_STRING define from top Makefile to trace-cmd-local.h.
]
Tzvetomir Stoyanov (VMware) (8):
trace-cmd: Move trace-cmd-local.h from the application to the library
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
Makefile | 3 -
include/trace-cmd/trace-cmd.h | 19 +-
.../include => include/trace-cmd}/trace-msg.h | 3 -
include/version.h | 5 -
lib/trace-cmd/Makefile | 2 +
.../trace-cmd}/include/trace-cmd-local.h | 7 +-
{tracecmd => lib/trace-cmd}/trace-msg.c | 4 +-
{tracecmd => lib/trace-cmd}/trace-output.c | 5 +-
lib/trace-cmd/trace-util.c | 162 ++++++++++++++++++
tracecmd/Makefile | 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 +-----
17 files changed, 218 insertions(+), 162 deletions(-)
rename {tracecmd/include => include/trace-cmd}/trace-msg.h (79%)
rename {tracecmd => lib/trace-cmd}/include/trace-cmd-local.h (83%)
rename {tracecmd => lib/trace-cmd}/trace-msg.c (99%)
rename {tracecmd => lib/trace-cmd}/trace-output.c (99%)Signature · 1 line
2.21.0
PATCH v2 1/8 trace-cmd: Move trace-cmd-local.h from the application to the library
The trace-cmd-local.h file is used by trace-input.c, trace-output.c and trace-msg.c files. The first one is part of the libtracecmd library. The others will be moved to the same library, so this header file should be part of the library too.
---
{tracecmd => lib/trace-cmd}/include/trace-cmd-local.h | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename {tracecmd => lib/trace-cmd}/include/trace-cmd-local.h (100%)lib/trace-cmd/include/trace-cmd-local.h renamed from tracecmd/include/trace-cmd-local.h +0 -0
Signature · 1 line
2.21.0
PATCH v2 2/8 trace-cmd: Move trace-output.c into the library code
In reply to 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
Since in trace-output.c is the only code, that uses FILE_VERSION_STRING define (the version of trace.dat file format), this define is moved from version.h in trace-cmd-local.h.
---
[
v2 changes:
- Removed trace-output.c dependency of version.h. Moved FILE_VERSION_STRING define from top Makefile to trace-cmd-local.h.
]
Makefile | 3 ---
include/version.h | 5 -----
lib/trace-cmd/Makefile | 1 +
lib/trace-cmd/include/trace-cmd-local.h | 7 +++++++
{tracecmd => lib/trace-cmd}/trace-output.c | 1 -
tracecmd/Makefile | 1 -
6 files changed, 8 insertions(+), 10 deletions(-)
rename {tracecmd => lib/trace-cmd}/trace-output.c (99%)Makefile +0 -3
@@ -10,9 +10,6 @@ export TC_PATCHLEVEL export TC_EXTRAVERSION export TRACECMD_VERSION -# file format version -FILE_VERSION = 6 - MAKEFLAGS += --no-print-directory # Makefiles suck: This macro sets a default value of $(2) for the
include/version.h +0 -5
@@ -9,9 +9,4 @@#include "tc_version.h" #endif -#define _STR (x) #x -#define STR (x) _STR(x) - -#define FILE_VERSION_STRING STR(FILE_VERSION) -#endif /* _VERSION_H */
lib/trace-cmd/Makefile +1 -0
@@ -10,6 +10,7 @@ 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/include/trace-cmd-local.h +7 -0
@@ -11,6 +11,13 @@#include "trace-cmd.h" #include "event-utils.h" +/* trace.dat file format version */ +#define FILE_VERSION 6 + +#define _STR (x) #x +#define STR (x) _STR(x) +#define FILE_VERSION_STRING STR(FILE_VERSION) +extern int quiet;static ssize_t __do_write (int fd,const void *data,size_t size)
lib/trace-cmd/trace-output.c renamed from tracecmd/trace-output.c +0 -1
@@ -23,7 +23,6 @@#include "trace-cmd-local.h" #include "list.h" #include "trace-msg.h" -#include "version.h" /* We can't depend on the host size for size_t, all must be 64 bit */ typedef unsigned long long tsize_t ;
tracecmd/Makefile +0 -1
@@ -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
PATCH v2 3/8 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
lib/trace-cmd/Makefile +1 -0
@@ -14,6 +14,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
tracecmd/Makefile +0 -1
@@ -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
PATCH v2 4/8 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/include/trace-cmd-local.h | 2 -- lib/trace-cmd/trace-output.c | 4 ++-- lib/trace-cmd/trace-util.c | 21 +++++++++++++++++++++ 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
@@ -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/include/trace-cmd-local.h +0 -2
@@ -18,8 +18,6 @@#define STR (x) _STR(x)#define FILE_VERSION_STRING STR(FILE_VERSION) -extern int quiet; -static ssize_t __do_write (int fd,const void *data,size_t size) {ssize_t tot =0 ;
lib/trace-cmd/trace-output.c +2 -2
@@ -1157,7 +1157,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 );
@@ -1172,7 +1172,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
@@ -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-local.h +0 -1
@@ -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
@@ -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
@@ -3387,7 +3387,7 @@ static void print_stat(struct buffer_instance *instance) {int cpu; -if (quiet) +if (tracecmd_get_quiet ())return ;if (!is_top_instance (instance))
@@ -4207,7 +4207,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); }
@@ -5154,7 +5154,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
Re: [PATCH v2 4/8] trace-cmd: Move trace-cmd global variable "quiet" to libtracecmd
Quoted 2 lines · On Wed, 14 Aug 2019 11:47:04 +0300 "Tzvetomir Stoyanov (VMware)"…
void
tracecmd_set_quiet(int quiet);
inttracecmd_get_quiet(void);
I rather make this a parameter to the descriptor:
tracecmd_set_quiet(struct tracecmd_output *handle, bool quiet); tracecmd_get_quiet(struct tracecmd output *handle);
As we may have multiple handles and perhaps we don't want all of them quiet.
-- Steve
Re: [PATCH v2 4/8] trace-cmd: Move trace-cmd global variable "quiet" to libtracecmd
Quoted 7 lines · Steven Rostedt
Quoted 2 lines · On Wed, 14 Aug 2019 11:47:04 +0300 "Tzvetomir Stoyanov (VMware)"…
void
tracecmd_set_quiet(int quiet);
inttracecmd_get_quiet(void);
Quoted 8 lines
I rather make this a parameter to the descriptor:
tracecmd_set_quiet(struct tracecmd_output *handle, bool quiet); tracecmd_get_quiet(struct tracecmd output *handle);As we may have multiple handles and perhaps we don't want all of them quiet.
It makes sense to have "quiet" per tracecmd_output handler, but when I started to modify the code,
I noticed one flow where tracecmd_get_quiet() is used and there is no tracecmd_output handler available -
in check_plugin(). We have either to drop the usage of tracecmd_get_quiet() in check_plugin(), or leave the scope of "quiet" to be for the whole library.
Quoted 1 line
-- Steve
Signature · 2 lines
Tzvetomir (Ceco) Stoyanov VMware Open Source Technology Center
Re: [PATCH v2 4/8] trace-cmd: Move trace-cmd global variable "quiet" to libtracecmd
Quoted 16 lines · On Thu, 29 Aug 2019 14:39:43 +0300 Tzvetomir Stoyanov <[email protected]>…
Quoted 7 lines · Steven Rostedt
Quoted 2 lines · On Wed, 14 Aug 2019 11:47:04 +0300 "Tzvetomir Stoyanov (VMware)"…
void
tracecmd_set_quiet(int quiet);
inttracecmd_get_quiet(void);Quoted 8 lines
I rather make this a parameter to the descriptor:
tracecmd_set_quiet(struct tracecmd_output *handle, bool quiet); tracecmd_get_quiet(struct tracecmd output *handle);As we may have multiple handles and perhaps we don't want all of them quiet.
Quoted 7 lines
It makes sense to have "quiet" per
tracecmd_outputhandler, but when I started to modify the code,
I noticed one flow wheretracecmd_get_quiet()is used and there is notracecmd_outputhandler available -
incheck_plugin(). We have either to drop the usage oftracecmd_get_quiet()incheck_plugin(), or leave the scope of "quiet" to be for the whole library.
We can still have a global variable in trace-record.c called quiet. And that gets set by the parameter:
case OPT_quite: case 'q': quiet = 1; break;
[..]
tracecmd_set_quiet(handle, quiet);
This is the proper way to handle it. The functions local to trace-record.c, can just use its own quiet state variable, but when we set quiet, we use the tracecmd_set_quiet() to notify the library that it too should be quiet.
-- Steve
PATCH v2 5/8 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -5143,7 +5143,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
Re: [PATCH v2 5/8] trace-cmd: Move trace-cmd global variable "debug" to libtracecmd
Quoted 6 lines · On Wed, 14 Aug 2019 11:47:05 +0300 "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:
voidtracecmd_set_debug(bool debug);
booltracecmd_get_debug(void);
I was thinking of doing a descriptor for this too, but screw it. Debug is debug ;-)
-- Steve
PATCH v2 6/8 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
@@ -398,6 +398,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
@@ -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
Re: [PATCH v2 6/8] trace-cmd: Move plog() function to libtracecmd.
Quoted 27 lines · On Wed, 14 Aug 2019 11:47:06 +0300 "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:
inttrace_set_log_file(char *logfile); - use it to set
the log file.
voidplog_error(const char *fmt, ...); - use it to log
an error message into the file.The
plog()function is used also frompdie()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 newplog()library function.Signed-off-by Tzvetomir Stoyanov (VMware) <[email protected]>--- 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 +0
-0
include/trace-cmd/trace-cmd.h struct hook_list { · line 402
Quoted 5 lines
include/trace-cmd/trace-cmd.h +2
-0@@ -398,6 +398,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, ...);
If these are going to become visible in the library, then they need to have a prefix "tracecmd_" attached to them.
Quoted 22 lines
include/trace-cmd/trace-cmd.h +2
-0+int trace_set_log_file (char *logfile); +/* --- Hack! --- */ int tracecmd_blk_hack (struct tracecmd_input *handle);include/trace-cmd/trace-msg.h +0
-3@@ -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 +0
-0
lib/trace-cmd/trace-util.c int tracecmd_disable_plugins; · line 34
Quoted 4 lines
lib/trace-cmd/trace-util.c +1
-0@@ -31,6 +31,8 @@ int tracecmd_disable_plugins;static int tracecmd_quiet;static bool tracecmd_debug; +static FILE *trace_logfp;
As this is a static variable, we only need to call it logfp.
Quoted 58 lines
lib/trace-cmd/trace-util.c +51
-0+ static struct registered_plugin_options { struct registered_plugin_options *next; struct tep_plugin_option *options;@@ -1716,3 +1718,72 @@ void __weak *malloc_or_die(unsigned int size)die ("malloc" );return data; } + +#define LOG_BUF_SIZE 1024 +static void __plog (const char *prefix,const char *fmt,va_list ap,FILE *fp) +{ +static int newline =1 ; +char buf[LOG_BUF_SIZE ]; +int r; + + r =vsnprintf (buf,LOG_BUF_SIZE , fmt, ap); + +if (r >LOG_BUF_SIZE ) + r =LOG_BUF_SIZE ; + +if (trace_logfp) { +if (newline) +fprintf (trace_logfp,"[%d]%s%.*s" ,getpid (), prefix, r, buf); +else +fprintf (trace_logfp,"[%d]%s%.*s" ,getpid (), prefix, r, buf); + newline = buf[r -1 ] =='\n' ; +fflush (trace_logfp); +return ; + } + +fprintf (fp,"%.*s" , r, buf); +} + +void plog (const char *fmt, ...) +{ +va_list ap; + +va_start (ap, fmt); +__plog ("" , fmt, ap, stdout); +va_end (ap); +/* Make sure it gets to the screen, in case we crash afterward */ +fflush (stdout); +} + +void plog_error (const char *fmt, ...) +{ +va_list ap; +char *str ="" ; + +va_start (ap, fmt); +__plog ("Error: " , fmt, ap, stderr); +va_end (ap); +if (errno) + str =strerror (errno); +if (trace_logfp) +fprintf (trace_logfp,"\n%s\n" , str); +else
lib/trace-cmd/trace-util.c void __weak *malloc_or_die(unsigned int size) · line 1780
Quoted 10 lines
lib/trace-cmd/trace-util.c +10
-0+fprintf (stderr,"\n%s\n" , str); +} + +/** +* trace_set_log_file - Set file for logging +* @logfile: Name of the log file +* +* Returns 0 on successful completion or -1 in case of error +*/ +int trace_set_log_file (char *logfile)
Should it be called tracecmd_set_logfile()?
-- Steve
Quoted 9 lines
lib/trace-cmd/trace-util.c +9
-0+{ +if (trace_logfp) +fclose (trace_logfp); + trace_logfp =fopen (logfile,"w" ); +if (!trace_logfp) +return -1 ; +return 0 ; +} +
PATCH v2 7/8 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
@@ -137,8 +137,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);
@@ -304,14 +302,7 @@ struct tracecmd_recorder *tracecmd_create_buffer_recorder_maxkb(const char *fileint 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
@@ -216,6 +216,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
Re: [PATCH v2 7/8] trace-cmd: Move trace-cmd APIs from trace-cmd.h to trace-local.h
Quoted 57 lines · On Wed, 14 Aug 2019 11:47:07 +0300 "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 intrace-cmd.h. The declarations are moved fromtrace-cmd.htotrace-local.h, local header file visible only to trace-cmd application.Signed-off-by Tzvetomir Stoyanov (VMware) <[email protected]>--- 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@@ -137,8 +137,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);@@ -304,14 +302,7 @@ struct tracecmd_recorder *tracecmd_create_buffer_recorder_maxkb(const char *fileint 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 +1
-0@@ -216,6 +216,17 @@ void show_instance_file(struct buffer_instance *instance, const char *name);int count_cpus (void ); +/* moved from trace-cmd.h */
tracecmd/include/trace-local.h void show_instance_file(struct buffer_instance *instance, const char *name); · line 229
Quoted 10 lines
tracecmd/include/trace-local.h +10
-0+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); +
I'll take this patch, but I wonder if we should change "tracecmd_" to "trace_" to let it be known that these are local functions. That can be a later patch.
-- Steve
Quoted 3 lines
tracecmd/include/trace-local.h +0
-0/* 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 */
PATCH v2 8/8 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
@@ -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
Re: [PATCH v2 8/8] trace-cmd: Move tracecmd_stack_tracer_status() function to libtracecmd
Quoted 25 lines · On Wed, 14 Aug 2019 11:47:08 +0300 "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.Signed-off-by Tzvetomir Stoyanov (VMware) <[email protected]>--- 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 +1
-0@@ -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 ;
lib/trace-cmd/trace-util.c int trace_set_log_file(char *logfile) · line 1798
Quoted 10 lines
lib/trace-cmd/trace-util.c +8
-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.
"(stack tracer not configured in kernel)"
Saying "enabled" is ambiguous.
lib/trace-cmd/trace-util.c int trace_set_log_file(char *logfile) · line 1800
Quoted 2 lines
lib/trace-cmd/trace-util.c +2
-0+*/ +int tracecmd_stack_tracer_status (int *status)
If we are making this a library function, shouldn't it be declared in the trace-cmd.h file?
-- Steve
Re: [PATCH v2 8/8] trace-cmd: Move tracecmd_stack_tracer_status() function to libtracecmd
Hi Steven,
Quoted 1 line · Steven Rostedt
...
Quoted 11 lines
Quoted 8 lines
+/**
+ *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."(stack tracer not configured in kernel)"
Quoted 8 lines
Saying "enabled" is ambiguous.
Quoted 2 lines
+ */
+inttracecmd_stack_tracer_status(int *status)If we are making this a library function, shouldn't it be declared in the
trace-cmd.hfile?
It is already part of trace-cmd.h, that was one of the problems - it was defined as library API, but implemented in the trace-cmd application. This patch moves the implementation in the library, the declaration in trace-cmd.h remains the same.
Quoted 1 line
-- Steve
Signature · 2 lines
Tzvetomir (Ceco) Stoyanov VMware Open Source Technology Center
v5 cover Add new trace-cmd record options: "--proc-map"
In reply to Tzvetomir Stoyanov (VMware)New options to trace-cmd record are added:
- "--proc-map" - Saves traced process(es) address map in trace.dat file.
- "--user" - Executes the traced process as given user.
As "--proc-map" option uses trace-cmd ptrace logic, part of this code is rewritten. There were some leftovers related to this logic, the code is upadted.
[
v5 changes:
- Added new patch:
"Extend ptrace logic to work with multiple filtered pids"
It resolves "filter_pid" leftover in ptrace related logic.
- "--proc-map" does not depend on option -F, it works with any command, specified as trace-cmd argument or option -P.
- Renamed "mmap" to "proc-map" - the option name and the names of the functions, variables and defines related to this feature.
v4 changes:
- Added check for strdup() failure.
- Made input user string argument of change_user() and run_cmd() constant.
- Added description of the new "--mmap" trace-cmd option in the program's help and the man page. (Suggested by Slavomir Kaslev)
Problems, reported by Yordan Karadzhov:
- Improved the parsing of /proc/<pid>/maps. Made it not so strict, as it failed on some machines due to different size of fields.
- Implemented trace_pid_mmap_free() cleanup function to free mmap related resources at trace-cmd exit.
- Fixed potential problem with non-terminated string, returned by readlink().
- Coding style fixes.
v3 changes:
- "--user" does not depend on option -F, it works with any command, specified as trace-cmd argument.
- Changed tracecmd_search_task_mmap() API to return not only the library
name, but also the start and end memory addresses.
- Renamed *tracee* to *task*
- Improved resources cleanup, in case of an error.
- Removed (this) changelog from the commit message.
v2 changes:
- Check for errors in change_user(). If an error occurs while changing the user, the message is printed and the traced process is not executed.
- Replaced usage of tracecmd_add_option_v() with tracecmd_add_option() API.
- Added checks to prevent buffer overflow when sscanf (... "%s", buf) is used.
- Return error in case memory allocation fails.
- Return error if option string is not in the expected format.
- Sort memory maps and use binary search to find matching library in the map.
]
Tzvetomir Stoyanov (VMware) (3):
trace-cmd: Extend ptrace logic to work with multiple filtered pids
trace-cmd: Save the tracee address map into the trace.dat file.
trace-cmd: Add option to execute traced process as given user
Documentation/trace-cmd-record.1.txt | 6 + include/trace-cmd/trace-cmd.h | 10 + lib/trace-cmd/trace-input.c | 172 +++++++++++++++- tracecmd/include/trace-local.h | 10 + tracecmd/trace-record.c | 282 +++++++++++++++++++++++++-- tracecmd/trace-usage.c | 2 + 6 files changed, 464 insertions(+), 18 deletions(-)
Signature · 1 line
2.21.0
Re: [PATCH v5 0/3] Add new trace-cmd record options: "--proc-map"
Quoted 8 lines · On Wed, 14 Aug 2019 11:47:09 +0300 "Tzvetomir Stoyanov (VMware)"…
New options to trace-cmd record are added:
- "--proc-map" - Saves traced process(es) address map in trace.dat file.
- "--user" - Executes the traced process as given user.As "--proc-map" option uses trace-cmd ptrace logic, part of this code is rewritten. There were some leftovers related to this logic, the code is upadted.
Hi Tzvetomir,
For some reason, this patch series ended up as part of the: Separate trace-cmd and libtracecmd code series.
-- Steve
Quoted 57 lines
[
v5 changes:
- Added new patch:
"Extend ptrace logic to work with multiple filtered pids"
It resolves "filter_pid" leftover in ptrace related logic.
- "--proc-map" does not depend on option -F, it works with any command, specified as trace-cmd argument or option -P.
- Renamed "mmap" to "proc-map" - the option name and the names of the functions, variables and defines related to this feature.
v4 changes:
- Added check forstrdup()failure.
- Made input user string argument ofchange_user()andrun_cmd()constant.
- Added description of the new "--mmap" trace-cmd option in the program's help and the man page. (Suggested by Slavomir Kaslev)Problems, reported by Yordan Karadzhov:
- Improved the parsing of /proc/<pid>/maps. Made it not so strict, as it failed on some machines due to different size of fields.
- Implementedtrace_pid_mmap_free()cleanup function to free mmap related resources at trace-cmd exit.
- Fixed potential problem with non-terminated string, returned byreadlink().
- Coding style fixes.
v3 changes:
- "--user" does not depend on option -F, it works with any command, specified as trace-cmd argument.
- Changedtracecmd_search_task_mmap()API to return not only the library
name, but also the start and end memory addresses.
- Renamed *tracee* to *task*
- Improved resources cleanup, in case of an error.
- Removed (this) changelog from the commit message.
v2 changes:
- Check for errors inchange_user(). If an error occurs while changing the user, the message is printed and the traced process is not executed.
- Replaced usage oftracecmd_add_option_v()withtracecmd_add_option()API.
- Added checks to prevent buffer overflow when sscanf (... "%s", buf) is used.
- Return error in case memory allocation fails.
- Return error if option string is not in the expected format.
- Sort memory maps and use binary search to find matching library in the map.
]Tzvetomir Stoyanov (VMware) (3):
trace-cmd: Extend ptrace logic to work with multiple filtered pids
trace-cmd: Save the tracee address map into the trace.dat file.
trace-cmd: Add option to execute traced process as given userDocumentation/trace-cmd-record.1.txt | 6 + include/trace-cmd/trace-cmd.h | 10 + lib/trace-cmd/trace-input.c | 172 +++++++++++++++- tracecmd/include/trace-local.h | 10 + tracecmd/trace-record.c | 282 +++++++++++++++++++++++++-- tracecmd/trace-usage.c | 2 + 6 files changed, 464 insertions(+), 18 deletions(-)
PATCH v5 1/3 trace-cmd: Extend ptrace logic to work with multiple filtered pids
In reply to Tzvetomir Stoyanov (VMware)In the trace-record.c file there is a global variable named "filter_pid". It is not set anywhere in the code, but there is a logic which relies on it. This variable is a leftover from the past implementation of trace-cmd record "-P" option, when it was designed to filter only a single PID. Now "-P" option works with a list of PIDs, stored in filter_pids global list. The code is modified to work with filter_pids instead of filter_pid. This logic is used only if there is no ftrace "options/event-fork" on the system and we have ptrace support. There is one significant change in the trace-cmd record behavior in this specific use case:
- filtered pids are specified with the "-P" option.
- there is no ftrace "options/event-fork" on the system.
- there is ptrace support.
The trace continues until Ctrl^C is hit or all filtered PIDs exit, whatever comes first.
--- tracecmd/trace-record.c | 65 ++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 13 deletions(-)
tracecmd/trace-record.c +52 -13
@@ -86,7 +86,6 @@ static bool use_tcp;static int do_ptrace;static int filter_task; -static int filter_pid =-1 ;static bool no_filter = false;static int local_cpu_count;
@@ -866,6 +865,13 @@ static void add_filter_pid(int pid, int exclude)struct filter_pids *p;char buf[100 ]; +for (p = filter_pids; p; p = p->next) { +if (p->pid == pid) { + p->exclude = exclude; +return ; + } + } + p =malloc (sizeof (*p));if (!p)die ("Failed to allocate pid filter" );
@@ -1223,17 +1229,34 @@ static void enable_ptrace(void)ptrace (PTRACE_TRACEME ,0 ,NULL ,0 ); } -static void ptrace_wait (enum trace_type type,int main_pid) +static void ptrace_wait (enum trace_type type) { +struct filter_pids *fpid;unsigned long send_sig;unsigned long child;siginfo_t sig; +int main_pids;int cstatus;int status; +int i =0 ; +int *pids;int event;int pid;int ret; + pids =calloc (nr_filter_pids,sizeof (int )); +if (!pids) +return ; + +for (fpid = filter_pids; fpid; fpid = fpid->next) { +if (fpid->exclude) +continue ; + pids[i++] = fpid->pid; +if (i >= nr_filter_pids) +break ; + } + main_pids = i; + do { ret = trace_waitpid(type, -1, &status, WSTOPPED | __WALL); if (ret < 0)
@@ -1275,11 +1298,24 @@ static void ptrace_wait(enum trace_type type, int main_pid) PTRACE_O_TRACEEXIT); ptrace(PTRACE_CONT, pid, NULL, send_sig); } - }while (!finished && ret >0 && - (!WIFEXITED (status) || pid != main_pid)); +if (WIFEXITED (status) || + (WIFSTOPPED (status) && event ==PTRACE_EVENT_EXIT )) { +for (i =0 ; i < nr_filter_pids; i++) { +if (pid == pids[i]) { + pids[i] =0 ; + main_pids--; +if (!main_pids) + finished =1 ; +break ; + } + } + } + }while (!finished && ret >0 ); + +free (pids); }#else -static inline void ptrace_wait (enum trace_type type,int main_pid) { } +static inline void ptrace_wait (enum trace_type type) { }static inline void enable_ptrace (void ) { }static inline void ptrace_attach (int pid) { }
@@ -1289,8 +1325,8 @@ static void trace_or_sleep(enum trace_type type) {struct timeval tv = {1 ,0 }; -if (do_ptrace && filter_pid >=0 ) -ptrace_wait (type, filter_pid); +if (do_ptrace && filter_pids) +ptrace_wait (type);else if (type &TRACE_TYPE_STREAM )trace_stream_read (pids, recorder_threads, &tv);else
@@ -1327,7 +1363,7 @@ static void run_cmd(enum trace_type type, int argc, char **argv) }if (do_ptrace) {add_filter_pid (pid,0 ); -ptrace_wait (type, pid); +ptrace_wait (type); }else trace_waitpid (type, pid, &status,0 ); }
@@ -2318,7 +2354,7 @@ create_event(struct buffer_instance *instance, char *path, struct event_list *ol *event = *old_event;add_event (instance, event); -if (event->filter || filter_task || filter_pid) { +if (event->filter || filter_task || filter_pids) { event->filter_file =strdup (path);if (!event->filter_file)die ("malloc filter file" );
@@ -4924,9 +4960,9 @@ static void parse_record_options(int argc,add_func (&ctx->instance->filter_funcs, ctx->instance->filter_mod,"*" ); -if (do_ptrace && !filter_task && (filter_pid <0 )) +if (do_ptrace && !filter_task && !nr_filter_pids)die (" -c can only be used with -F (or -P with event-fork support)" ); -if (ctx->do_child && !filter_task &&! filter_pid) +if (ctx->do_child && !filter_task && !nr_filter_pids)die (" -c can only be used with -P or -F" );if ((argc - optind) >=2 ) {
@@ -4997,6 +5033,7 @@ static void record_trace(int argc, char **argv, {enum trace_type type =get_trace_cmd_type (ctx->curr_cmd);struct buffer_instance *instance; +struct filter_pids *pid;/* * If top_instance doesn't have any plugins or events, then
@@ -5083,8 +5120,10 @@ static void record_trace(int argc, char **argv,update_task_filter ();tracecmd_enable_tracing ();/* We don't ptrace ourself */ -if (do_ptrace && filter_pid >=0 ) -ptrace_attach (filter_pid); +if (do_ptrace && filter_pids) +for (pid = filter_pids; pid; pid = pid->next) +if (!pid->exclude) +ptrace_attach (pid->pid);/* sleep till we are woken with Ctrl^C */ printf ("Hit Ctrl^C to stop recording\n" );while (!finished)
Signature · 1 line
2.21.0
Re: [PATCH v5 1/3] trace-cmd: Extend ptrace logic to work with multiple filtered pids
Quoted 58 lines · On Wed, 14 Aug 2019 11:47:10 +0300 "Tzvetomir Stoyanov (VMware)"…
In the
trace-record.cfile there is a global variable named "filter_pid". It is not set anywhere in the code, but there is a logic which relies on it. This variable is a leftover from the past implementation of trace-cmd record "-P" option, when it was designed to filter only a single PID. Now "-P" option works with a list of PIDs, stored infilter_pidsglobal list. The code is modified to work withfilter_pidsinstead offilter_pid. This logic is used only if there is no ftrace "options/event-fork" on the system and we have ptrace support. There is one significant change in the trace-cmd record behavior in this specific use case:
- filtered pids are specified with the "-P" option.
- there is no ftrace "options/event-fork" on the system.
- there is ptrace support.
The trace continues until Ctrl^C is hit or all filtered PIDs exit, whatever comes first.Signed-off-by Tzvetomir Stoyanov (VMware) <[email protected]>--- tracecmd/trace-record.c | 65 ++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 13 deletions(-)tracecmd/trace-record.c +10
-2@@ -86,7 +86,6 @@ static bool use_tcp;static int do_ptrace;static int filter_task; -static int filter_pid =-1 ;static bool no_filter = false;static int local_cpu_count;@@ -866,6 +865,13 @@ static void add_filter_pid(int pid, int exclude)struct filter_pids *p;char buf[100 ]; +for (p = filter_pids; p; p = p->next) { +if (p->pid == pid) { + p->exclude = exclude; +return ; + } + } + p =malloc (sizeof (*p));if (!p)die ("Failed to allocate pid filter" );@@ -1223,17 +1229,34 @@ static void enable_ptrace(void)ptrace (PTRACE_TRACEME ,0 ,NULL ,0 ); } -static void ptrace_wait (enum trace_type type,int main_pid) +static void ptrace_wait (enum trace_type type) { +struct filter_pids *fpid;unsigned long send_sig;unsigned long child;siginfo_t sig; +int main_pids;
tracecmd/trace-record.c static void enable_ptrace(void) · line 1248
Quoted 10 lines
tracecmd/trace-record.c +4
-0int cstatus;int status; +int i =0 ; +int *pids;int event;int pid;int ret; + pids =calloc (nr_filter_pids,sizeof (int )); +if (!pids)
Probably at the minimum, we should add a warning here that it didn't get allocated.
Quoted 92 lines
tracecmd/trace-record.c +34
-9+return ; + +for (fpid = filter_pids; fpid; fpid = fpid->next) { +if (fpid->exclude) +continue ; + pids[i++] = fpid->pid; +if (i >= nr_filter_pids) +break ; + } + main_pids = i; + do { ret = trace_waitpid(type, -1, &status, WSTOPPED | __WALL); if (ret < 0)@@ -1275,11 +1298,24 @@ static void ptrace_wait(enum trace_type type, int main_pid) PTRACE_O_TRACEEXIT); ptrace(PTRACE_CONT, pid, NULL, send_sig); } - }while (!finished && ret >0 && - (!WIFEXITED (status) || pid != main_pid)); +if (WIFEXITED (status) || + (WIFSTOPPED (status) && event ==PTRACE_EVENT_EXIT )) { +for (i =0 ; i < nr_filter_pids; i++) { +if (pid == pids[i]) { + pids[i] =0 ; + main_pids--; +if (!main_pids) + finished =1 ; +break ; + } + } + } + }while (!finished && ret >0 ); + +free (pids); }#else -static inline void ptrace_wait (enum trace_type type,int main_pid) { } +static inline void ptrace_wait (enum trace_type type) { }static inline void enable_ptrace (void ) { }static inline void ptrace_attach (int pid) { }@@ -1289,8 +1325,8 @@ static void trace_or_sleep(enum trace_type type) {struct timeval tv = {1 ,0 }; -if (do_ptrace && filter_pid >=0 ) -ptrace_wait (type, filter_pid); +if (do_ptrace && filter_pids) +ptrace_wait (type);else if (type &TRACE_TYPE_STREAM )trace_stream_read (pids, recorder_threads, &tv);else @@ -1327,7 +1363,7 @@ static void run_cmd(enum trace_type type, int argc, char **argv) }if (do_ptrace) {add_filter_pid (pid,0 ); -ptrace_wait (type, pid); +ptrace_wait (type); }else trace_waitpid (type, pid, &status,0 ); }@@ -2318,7 +2354,7 @@ create_event(struct buffer_instance *instance, char *path, struct event_list *ol *event = *old_event;add_event (instance, event); -if (event->filter || filter_task || filter_pid) { +if (event->filter || filter_task || filter_pids) { event->filter_file =strdup (path);if (!event->filter_file)die ("malloc filter file" );@@ -4924,9 +4960,9 @@ static void parse_record_options(int argc,add_func (&ctx->instance->filter_funcs, ctx->instance->filter_mod,"*" ); -if (do_ptrace && !filter_task && (filter_pid <0 )) +if (do_ptrace && !filter_task && !nr_filter_pids)die (" -c can only be used with -F (or -P with event-fork support)" ); -if (ctx->do_child && !filter_task &&! filter_pid) +if (ctx->do_child && !filter_task && !nr_filter_pids)die (" -c can only be used with -P or -F" );if ((argc - optind) >=2 ) {@@ -4997,6 +5033,7 @@ static void record_trace(int argc, char **argv, {enum trace_type type =get_trace_cmd_type (ctx->curr_cmd);struct buffer_instance *instance; +struct filter_pids *pid;/* * If top_instance doesn't have any plugins or events, then
tracecmd/trace-record.c static void record_trace(int argc, char **argv, · line 5126
Quoted 9 lines
tracecmd/trace-record.c +4
-2@@ -5083,8 +5120,10 @@ static void record_trace(int argc, char **argv,update_task_filter ();tracecmd_enable_tracing ();/* We don't ptrace ourself */ -if (do_ptrace && filter_pid >=0 ) -ptrace_attach (filter_pid); +if (do_ptrace && filter_pids) +for (pid = filter_pids; pid; pid = pid->next) +if (!pid->exclude) +ptrace_attach (pid->pid);
Just a nit, the above should have brackets. Leaving off brackets is fine for non complex blocks. That is, only the internal if should have no brackets:
if (do_ptrace && filter_pids) {
for (pid = filter_pids; pid; pid = pid->next) {
if (!pid->exclude)
ptrace_attach(pid->pid);
}
}Otherwise mistakes can easily be made.
-- Steve
Quoted 3 lines
tracecmd/trace-record.c +0
-0/* sleep till we are woken with Ctrl^C */ printf ("Hit Ctrl^C to stop recording\n" );while (!finished)
PATCH v5 2/3 trace-cmd: Save the tracee address map into the trace.dat file.
In reply to Tzvetomir Stoyanov (VMware)A new trace-cmd record option is added: "--proc-map". When it is set the address map of the traced applications is stored in the trace.dat file. The traced applications can be specified using the option -P, or as a given 'command'. A new API tracecmd_search_task_map() can be used to look up into stored address maps. The map is retrieved from /proc/<pid>/maps file.
---
[
v5 changes:
- Added new patch:
"Extend ptrace logic to work with multiple filtered pids"
It resolves "filter_pid" leftover in ptrace related logic.
- "--proc-map" does not depend on option -F, it works with any command, specified as trace-cmd argument or option -P.
- Renamed "mmap" to "proc-map" - the option name and the names of the functions, variables and defines related to this feature.
v4 changes:
- Added description of the new "--mmap" trace-cmd option in the program's help and the man page. (Suggested by Slavomir Kaslev)
Problems, reported by Yordan Karadzhov:
- Improved the parsing of /proc/<pid>/maps. Made it not so strict, as it failed on some machines due to different size of fields.
- Implemented trace_pid_mmap_free() cleanup function to free mmap related resources at trace-cmd exit.
- Fixed potential problem with non-terminated string, returned by
readlink().
- Coding style fixes.
v3 changes:
- Changed tracecmd_search_task_mmap() API to return not only the library name, but also the start and end memory addresses.
- Renamed *tracee* to *task*
- Improved resources cleanup, in case of an error.
- Removed (this) changelog from the commit message.
v2 changes:
- Replaced usage of tracecmd_add_option_v() with tracecmd_add_option() API.
- Added checks to prevent buffer overflow when sscanf (... "%s", buf) is used.
- Return error in case memory allocation fails.
- Return error if option string is not in the expected format.
- Sort memory maps and use binary search to find matching library in the map.
]
Documentation/trace-cmd-record.1.txt | 3 + include/trace-cmd/trace-cmd.h | 10 ++ lib/trace-cmd/trace-input.c | 172 ++++++++++++++++++++++++++- tracecmd/include/trace-local.h | 10 ++ tracecmd/trace-record.c | 172 ++++++++++++++++++++++++++- tracecmd/trace-usage.c | 1 + 6 files changed, 364 insertions(+), 4 deletions(-)
Documentation/trace-cmd-record.1.txt +3 -0
@@ -288,6 +288,9 @@ OPTIONS '--module snd -n "*"' is equivalent to '-n :mod:snd' +*--proc-map*:: + Save the traced process address map into the trace.dat file. The traced + processes can be specified using the option *-P*, or as a given 'command'. *--profile*:: With the *--profile* option, "trace-cmd" will enable tracing that can
include/trace-cmd/trace-cmd.h +10 -0
@@ -82,6 +82,7 @@ enum {TRACECMD_OPTION_OFFSET ,TRACECMD_OPTION_CPUCOUNT ,TRACECMD_OPTION_VERSION , + TRACECMD_OPTION_PROCMAPS, }; enum {
@@ -97,6 +98,12 @@ struct tracecmd_ftrace {int long_size; }; +struct tracecmd_proc_addr_map { +unsigned long long start; +unsigned long long end; +char *lib_name; +}; +typedef void (*tracecmd_show_data_func )(struct tracecmd_input *handle,struct tep_record *record); typedef void (*tracecmd_handle_init_func)(struct tracecmd_input *handle,
@@ -208,6 +215,9 @@ unsigned long long tracecmd_page_ts(struct tracecmd_input *handle,unsigned int tracecmd_record_ts_delta (struct tracecmd_input *handle,struct tep_record *record); +struct tracecmd_proc_addr_map * +tracecmd_search_task_map (struct tracecmd_input *handle, +int pid,unsigned long long addr);#ifndef SWIG /* hack for function graph work around */ extern __threadstruct tracecmd_input *tracecmd_curr_thread_handle;
lib/trace-cmd/trace-input.c +169 -3
202 lines not on this page · raw message
tracecmd/include/trace-local.h +10 -0
@@ -157,6 +157,14 @@ struct func_list {const char *mod; }; +struct pid_addr_maps { +struct pid_addr_maps *next; +struct tracecmd_proc_addr_map *lib_maps; +unsigned int nr_lib_maps; +char *proc_name; +int pid; +}; +struct buffer_instance {struct buffer_instance *next;const char *name;
@@ -183,6 +191,8 @@ struct buffer_instance {struct tracecmd_msg_handle *msg_handle;struct tracecmd_output *network_handle; +struct pid_addr_maps *pid_maps; +char *max_graph_depth;int flags;
tracecmd/trace-record.c +171 -1
250 lines not on this page · raw message
tracecmd/trace-usage.c +1 -0
@@ -57,6 +57,7 @@ static struct usage_help usage_help[] = {" (use with caution)\n" " --max-graph-depth limit function_graph depth\n" " --no-filter include trace-cmd threads in the trace\n" +" --proc-map save the traced processes address map into the trace.dat file\n" }, { "start",
Signature · 1 line
2.21.0
PATCH v5 3/3 trace-cmd: Add option to execute traced process as given user
In reply to Tzvetomir Stoyanov (VMware)A new trace-cmd record option is added: "--user". When it is set with combination of option -F, the traced process is executed in the context of the specified user.
---
[
v4 changes:
- Added check for strdup() failure.
- Made input user string argument of change_user() and run_cmd() constant.
v3 changes:
"--user" does not depend on option -F, it works with any command,
specified as trace-cmd argument.
v2 changes:
- Check for errors in change_user(). If an error occurs while changing the user, the message is printed and the traced process is not executed.
]
Documentation/trace-cmd-record.1.txt | 3 ++ tracecmd/trace-record.c | 49 ++++++++++++++++++++++++++-- tracecmd/trace-usage.c | 1 + 3 files changed, 50 insertions(+), 3 deletions(-)
Documentation/trace-cmd-record.1.txt +3 -0
@@ -119,6 +119,9 @@ OPTIONS Used with either *-F* (or *-P* if kernel supports it) to trace the process' children too. +*--user*:: + Execute the specified *command* as given user. + *-C* 'clock':: Set the trace clock to "clock".
tracecmd/trace-record.c +46 -3
103 lines not on this page · raw message
tracecmd/trace-usage.c +1 -0
@@ -58,6 +58,7 @@ static struct usage_help usage_help[] = {" --max-graph-depth limit function_graph depth\n" " --no-filter include trace-cmd threads in the trace\n" " --proc-map save the traced processes address map into the trace.dat file\n" +" --user execute the specified [command ...] as given user\n" }, { "start",
Signature · 1 line
2.21.0
Quoted 4 lines · On Wed, 14 Aug 2019 11:47:00 +0300 "Tzvetomir Stoyanov (VMware)"…
libtracecmd is a library, containing functions that can be used without the trace-cmd application. However, some of the functions declared as libtracecmd APIs in
trace-cmd.hdepend on trace-cmd context. That causes a problem when
Quoted 9 lines
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 fromtrace-cmd.hfile.
I pulled in some but not all of these patches (the ones I had comments on). Feel free to rebase on the git repo and you only need to send the ones that have not been applied.
-- Steve