From: Tzvetomir Stoyanov <tz.stoyanov@gmail.com>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: linux-trace-devel@vger.kernel.org
Subject: Re: [PATCH v2 4/4] trace-cmd: Do not try to update parent's memory from a fork()-ed child
Date: Tue, 5 May 2020 11:39:50 +0300 [thread overview]
Message-ID: <CAPpZLN4XFrqtvnOSkACfkyUNAm691ceEMT9LBBBZezwXc6OnVA@mail.gmail.com> (raw)
In-Reply-To: <20200504163001.0ce5629d@gandalf.local.home>
Hi Steven,
On Mon, May 4, 2020 at 11:30 PM Steven Rostedt <rostedt@goodmis.org> wrote:
>
> On Mon, 4 May 2020 09:27:11 +0300
> "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com> wrote:
>
> > When trace-cmd runs a command, specified with the "-F" flag, it forks a
> > child process and executes the command in its context. This child process
> > receives a full copy of the parents memory at the moment of fork(). When
> > it modifies this copy, the parent memory is not affected. Calling the
> > function update_task_filter() in the child context will operate on a valid
> > data, but will not update anything in the parent's databases.
> >
> > Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
> > ---
> > tracecmd/trace-record.c | 64 +++++++++++++++++++++++++++++++++--------
> > 1 file changed, 52 insertions(+), 12 deletions(-)
> >
> > diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
> > index 1e4d38fa..ae8a5745 100644
> > --- a/tracecmd/trace-record.c
> > +++ b/tracecmd/trace-record.c
> > @@ -11,6 +11,7 @@
> > #include <stdarg.h>
> > #include <getopt.h>
> > #include <time.h>
> > +#include <semaphore.h>
>
> OK, first things first. Do not use semaphores. I think I mentioned this to
> you before. Semaphores are a horrible interface, and should be avoided at
> all costs! ;-)
>
> Also, I don't think you are solving the bug you think you are ;-)
>
While modifying the code for per instance PID filtering, I changed the ptrace()
related logic and decided to test these changes with "--proc-map". I looked at
the implementation to find out how ptrace() works when a program is filtered and
reached to this update_task_filter() call in the child context. I
thought it should
update the filtered tasks with child's PID and realized it does not update the
parent's list because it runs in the child context.
That's the reason for this patch, I agree there is no actual bug. As I
understand,
the update_task_filter() is called in child context to update ftrace
filter configuration
with the child's PID, not to update some trace-cmd internal state. That's why
add_filter_pid() is called again in the parent's context.
As there is no actual bug, and you prefer to avoid any semaphores in trace-cmd,
I'll withdraw the patch.
> With today's code (without this patch), I can run:
>
> # trace-cmd record -e exceptions -e sched -e irq --proc-map ls
>
> And for that result I can do:
>
> # trace-cmd dump --options
> [..]
> [Option PROCMAPS, 2383 bytes]
> a10 30 /usr/bin/ls
> 556850495000 556850499000 /usr/bin/ls
> 556850499000 5568504ad000 /usr/bin/ls
> 5568504ad000 5568504b6000 /usr/bin/ls
> 5568504b6000 5568504b8000 /usr/bin/ls
> 5568504b8000 5568504b9000 /usr/bin/ls
> 556850c60000 556850c81000 [heap]
> 7efce4a9b000 7efcf1a45000 /usr/lib/locale/locale-archive
> 7efcf1a49000 7efcf1a4f000 /usr/lib64/libpthread-2.28.so
> 7efcf1a4f000 7efcf1a5f000 /usr/lib64/libpthread-2.28.so
> 7efcf1a5f000 7efcf1a65000 /usr/lib64/libpthread-2.28.so
> 7efcf1a65000 7efcf1a66000 /usr/lib64/libpthread-2.28.so
> 7efcf1a66000 7efcf1a67000 /usr/lib64/libpthread-2.28.so
> [..]
>
> What is it that you are fixing? Remember, if we run --proc-map, we enable
> ptrace. Which at the end of its execution we have:
>
> case PTRACE_EVENT_EXIT:
> if (get_procmap)
> get_pid_addr_maps(pid);
>
> Where the code records the proc_map of the -F process when it exits.
>
> The only thing this patch is saving, is the wasted time of updating the
> procmaps from the child. And to stop that, all you need is this:
>
> diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
> index 1e4d38fa..4d647887 100644
> --- a/tracecmd/trace-record.c
> +++ b/tracecmd/trace-record.c
> @@ -1187,7 +1187,7 @@ static void get_filter_pid_maps(void)
> }
> }
>
> -static void update_task_filter(void)
> +static void update_task_filter(bool do_procmaps)
> {
> struct buffer_instance *instance;
> int pid = getpid();
> @@ -1195,7 +1195,7 @@ static void update_task_filter(void)
> if (no_filter)
> return;
>
> - if (get_procmap && filter_pids)
> + if (do_procmaps && get_procmap && filter_pids)
> get_filter_pid_maps();
>
> if (filter_task)
> @@ -1496,7 +1496,7 @@ static void run_cmd(enum trace_type type, const char *user, int argc, char **arg
> die("failed to fork");
> if (!pid) {
> /* child */
> - update_task_filter();
> + update_task_filter(false);
> tracecmd_enable_tracing();
> enable_ptrace();
> /*
> @@ -6285,7 +6285,7 @@ static void record_trace(int argc, char **argv,
> if (!latency)
> start_threads(type, ctx);
> } else {
> - update_task_filter();
> + update_task_filter(true);
> tracecmd_enable_tracing();
> exit(0);
> }
> @@ -6293,11 +6293,11 @@ static void record_trace(int argc, char **argv,
> if (ctx->run_command) {
> run_cmd(type, ctx->user, (argc - optind) - 1, &argv[optind + 1]);
> } else if (ctx->instance && is_agent(ctx->instance)) {
> - update_task_filter();
> + update_task_filter(true);
> tracecmd_enable_tracing();
> tracecmd_msg_wait_close(ctx->instance->msg_handle);
> } else {
> - update_task_filter();
> + update_task_filter(true);
> tracecmd_enable_tracing();
> /* We don't ptrace ourself */
> if (do_ptrace && filter_pids) {
>
> -- Steve
--
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
prev parent reply other threads:[~2020-05-05 8:40 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-05-04 6:27 [PATCH v2 0/4] Few small trace-cmd fixes Tzvetomir Stoyanov (VMware)
2020-05-04 6:27 ` [PATCH v2 1/4] trace-cmd: Fix trace-cmd report crash while displaying trace.dat in specific use case Tzvetomir Stoyanov (VMware)
2020-05-04 20:00 ` Steven Rostedt
2020-05-04 6:27 ` [PATCH v2 2/4] trace-cmd: Add "main" in the output of trace-cmd stat when displaying main instance Tzvetomir Stoyanov (VMware)
2020-05-04 13:41 ` Steven Rostedt
2020-05-04 6:27 ` [PATCH v2 3/4] trace-cmd: Create ftrace instances before using them Tzvetomir Stoyanov (VMware)
2020-05-04 6:27 ` [PATCH v2 4/4] trace-cmd: Do not try to update parent's memory from a fork()-ed child Tzvetomir Stoyanov (VMware)
2020-05-04 20:30 ` Steven Rostedt
2020-05-05 8:39 ` Tzvetomir Stoyanov [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAPpZLN4XFrqtvnOSkACfkyUNAm691ceEMT9LBBBZezwXc6OnVA@mail.gmail.com \
--to=tz.stoyanov@gmail.com \
--cc=linux-trace-devel@vger.kernel.org \
--cc=rostedt@goodmis.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).