Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 24 Feb 2014 20:59:03 -0500
From:      Mark Johnston <markj@freebsd.org>
To:        Adam Leventhal <ahl@delphix.com>
Cc:        freebsd-dtrace@freebsd.org
Subject:   Re: [patch] fasttrap process scratch space
Message-ID:  <20140225015903.GB64934@raichu>
In-Reply-To: <CALWP=0bnihYO1%2B4Zy9mtuNBtETh0WTBqj=jFP4z2JgqDDnDsMg@mail.gmail.com>
References:  <20140224041454.GB2720@raichu> <CALWP=0bnihYO1%2B4Zy9mtuNBtETh0WTBqj=jFP4z2JgqDDnDsMg@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, Feb 24, 2014 at 09:51:13AM -0800, Adam Leventhal wrote:
> Hey Mark,
> 
> I wanted to provide a little historical perspective when considering
> the options of a pact with libc/libthread/ld.so.1 vs. the kernel
> mapping pages for TLS.
> 
> We considered both but chose the former in Solaris.
> Solaris/OpenSolaris/illumos have the kernel, libc, and ld.so.1 in the
> same repository. There are already dependencies between those
> components, so adding another was not a concern.

That makes sense. Obviously, the same is true on FreeBSD, but what
mostly worried me is the lack of any way to determine whether the
traced process does in fact have scratch space available in its TLS.
What if the executable is statically-linked with a libthr without my
change? What if I'm attempting to trace a Linux binary running in
the compatibility layer? In these cases, the program will just crash
once it tries to execute instructions in non-executable memory (or it'll
corrupt the thread control block), and I don't see any way to detect or
prevent that in fasttrap.

FreeBSD's DTrace implementation also tries to be somewhat
compartmentalized so that it's possible to remove or add DTrace support
without too much work. To my knowledge, it's all currently implemented
using kernel modules and some userland executables and libraries.
Requiring libthr and rtld support would take us in the opposite
direction.

> We felt that having
> DTrace map pages into the traced process could have a more significant
> impact on its execution, and we wanted to minimize the chance that
> DTrace would chase away the very problem users were trying to
> investigate. Further, we felt that the failure modes would be less
> clean; for example, in your patch if we fail to map a page while in
> fasttrap_pid_probe(), we're forced to silently remove the
> instrumentation.

That's true. It seemed to me that having to map 4 KB for every 64
threads in the process is not too much overhead, but it'd certainly be
preferable to avoid it. In your much wider experience with userland
DTrace, do you know of use cases where this might be likely to cause
problems?

> 
> Hope that's helpful.

It is, I appreciate the explanation. I didn't mean to imply that the
solution used in Solaris is inferior to the approch I followed; I just
felt that it's not so well-suited to FreeBSD.

Thanks!
-Mark

> 
> Adam
> 
> On Sun, Feb 23, 2014 at 8:14 PM, Mark Johnston <markj@freebsd.org> wrote:
> > Hello,
> >
> > For those not familiar with MD parts of fasttrap, one of the things it
> > has to do is ensure that any userland instruction that it replaces with
> > a breakpoint gets executed in the traced process' context. For several
> > common classes of instructions, fasttrap will emulate the instruction in
> > the breakpoint handler; when it can't do that, it copies the instruction
> > out to some scratch space in the process' address space and sets the PC
> > of the interrupted thread to the address of that instruction, which is
> > followed by a jump to the instruction following the breakpoint. There's
> > a helpful block comment titled "Generic Instruction Tracing" around line
> > 1585 of the x86 fasttrap_isa.c which describes the details of this.
> >
> > This functionality currently doesn't work on FreeBSD, mainly because we
> > don't necessarily have any (per-thread) scratch space available for use
> > in the process' address space. In illumos/Solaris, a small (< 64 byte)
> > block is reserved in each thread's TLS for use by DTrace. It turns out
> > that doing the same thing on FreeBSD is quite easy:
> >
> > http://people.freebsd.org/~markj/patches/fasttrap_scratch_hacky.diff
> >
> > Specifically, we need to ensure that TLS (allocated by the runtime
> > linker) is executable and that we properly extract the offset to the
> > scratch space from the FS segment register. I think this is somewhat
> > hacky though, as it creates a dependency on libthr and rtld internals.
> >
> > A second approach is to have fasttrap dynamically allocate scratch space
> > within the process' address space using vm_map_insert(9). My
> > understanding is that Apple's DTrace implementation does this, and I've
> > implemented this approach for FreeBSD here (which was done without
> > referencing Apple code):
> >
> > http://people.freebsd.org/~markj/patches/fasttrap-scratch-space/fasttrap-scratch-space-1.diff
> >
> > The idea is to map pages of executable memory into the user process as
> > needed, and carve them into scratch space chunks for use by individual
> > threads. If a thread in fasttrap_pid_probe() needs scratch space, it
> > calls a new function, fasttrap_scraddr(). If the thread already has
> > scratch space allocated to it, it's used. Otherwise, if any free scratch
> > space chunks are available in an already-mapped page, one of them is
> > allocated to the thread and used. Otherwise, a new page is mapped using
> > vm_map_insert(9).
> >
> > Threads hold onto their scratch space until they exit. That is, scratch
> > space is never unmapped from the process, even if the controlling
> > dtrace(1) process detaches. I added a handler for thread_dtor event
> > which re-adds any scratch space held by the thread to the free list for
> > that process. Per-process scratch space state is held in the fasttrap
> > process handle (fasttrap_proc_t), since that turns out to be much easier
> > than keeping it in the struct proc.
> >
> > Does anyone have any thoughts or comments on the approach or the patch?
> > Any review or testing would be very much appreciated.
> >
> > For testing purposes, it's helpful to know that tracing memcpy() on
> > amd64 will result in use of this scratch space code, as it starts with a
> > "mov %rdi,%rax" on my machine at least. My main test case has been to
> > run something like
> >
> > # dtrace -n 'pid$target:libc.so.7::entry {@[probefunc] = count()}' -p $(pgrep firefox)
> >
> > Attempting to trace all functions still results in firefox dying with
> > SIGTRAP, but we're getting there. :)
> >
> > Thanks,
> > --
> > -Mark
> > _______________________________________________
> > freebsd-dtrace@freebsd.org mailing list
> > https://lists.freebsd.org/mailman/listinfo/freebsd-dtrace
> > To unsubscribe, send any mail to "freebsd-dtrace-unsubscribe@freebsd.org"
> 
> 
> 
> -- 
> Adam Leventhal
> CTO, Delphix
> http://blog.delphix.com/ahl

-- 
-Mark



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20140225015903.GB64934>