Skip to contentSkip to conversation

Add new trace-cmd record options: "--proc-map"

PATCH In review Tzvetomir Stoyanov (VMware) 2 participants 6 messages Aug 28, 2019 – Mar 2, 2020 Documentation

lore
Share & tools
lore
#0 lore raw

v6 cover Add new trace-cmd record options: "--proc-map"

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.

[
v6 changes:
- Added warning message when allocation of pids array fails.
- Put extra brackets around complex blocks in record_trace(), as Steven Rostedt suggested.
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              | 286 +++++++++++++++++++++++++--
 tracecmd/trace-usage.c               |   2 +
 6 files changed, 468 insertions(+), 18 deletions(-)
Signature · 1 line
2.21.0
#1 lore raw

PATCH v6 1/3 trace-cmd: Extend ptrace logic to work with multiple filtered pids

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 | 69 +++++++++++++++++++++++++++++++++--------
 1 file changed, 56 insertions(+), 13 deletions(-)
tracecmd/trace-record.c +56 -13
index 5dc6f17..917f52a 100644
@@ -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,36 @@ 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) {
+		warning("Unable to allocate array for %d PIDs", nr_filter_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 +1300,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 +1327,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 +1365,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 +2356,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 +4962,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 +5035,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 +5122,12 @@ 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
#2 lore raw

PATCH v6 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.

---
 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
index 26a8299..e697f03 100644
@@ -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
index 6f62ab9..c4a437a 100644
@@ -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 __thread struct 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
index 1cad3cc..78c52dc 100644
@@ -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
index 406384c..7a67784 100644
@@ -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
#3 lore raw

Re: [PATCH v6 2/3] trace-cmd: Save the tracee address map into the trace.dat file.

lib/trace-cmd/trace-input.c static int handle_options(struct tracecmd_input *handle) · line 2228

Quoted 7 lines · On Wed, 28 Aug 2019 14:24:56 +0300 "Tzvetomir Stoyanov (VMware)"…
lib/trace-cmd/trace-input.c +0 -3
@@ -2223,9 +2385,6 @@ static int handle_options(struct tracecmd_input *handle)
 		case TRACECMD_OPTION_UNAME:
 			handle->uname = strdup(buf);
 			break;
-		case TRACECMD_OPTION_VERSION:
-			handle->version = strdup(buf);
-			break;

I've been wondering why I've been getting "unknown option 9" for some time. Was there any reason to remove this?

-- Steve

Quoted 4 lines
lib/trace-cmd/trace-input.c +0 -0
 		case TRACECMD_OPTION_HOOK:
 			hook = tracecmd_create_event_hook(buf);
 			hook->next = handle->hooks;
@@ -2235,6 +2394,10 @@ static int handle_options(struct tracecmd_input *handle)
#4 lore raw

Re: [PATCH v6 2/3] trace-cmd: Save the tracee address map into the trace.dat file.

Quoted 4 lines · Steven Rostedt

On Wed, 28 Aug 2019 14:24:56 +0300
"Tzvetomir Stoyanov (VMware)" <[email protected]> wrote:

Quoted 10 lines

lib/trace-cmd/trace-input.c static int handle_options(struct tracecmd_input *handle) · line 2228

Quoted 7 lines
lib/trace-cmd/trace-input.c +0 -3
@@ -2223,9 +2385,6 @@ static int handle_options(struct tracecmd_input *handle)
              case TRACECMD_OPTION_UNAME:
                      handle->uname = strdup(buf);
                      break;
-             case TRACECMD_OPTION_VERSION:
-                     handle->version = strdup(buf);
-                     break;

I've been wondering why I've been getting "unknown option 9" for some time. Was there any reason to remove this?

It looks like a merging issue, I cannot remember any logical reason to remove it intentionally.

Quoted 8 lines

-- Steve

lib/trace-cmd/trace-input.c static int handle_options(struct tracecmd_input *handle)

Quoted 4 lines
lib/trace-cmd/trace-input.c +0 -0
              case TRACECMD_OPTION_HOOK:
                      hook = tracecmd_create_event_hook(buf);
                      hook->next = handle->hooks;
@@ -2235,6 +2394,10 @@ static int handle_options(struct tracecmd_input *handle)

--
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center

#5 lore raw

PATCH v6 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.

---
 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
index e697f03..bb18e88 100644
@@ -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
index 7a67784..20cb66f 100644
@@ -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