From owner-freebsd-dtrace@FreeBSD.ORG Mon Feb 24 17:51:14 2014 Return-Path: Delivered-To: freebsd-dtrace@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id A2626E8F for ; Mon, 24 Feb 2014 17:51:14 +0000 (UTC) Received: from mail-ob0-x229.google.com (mail-ob0-x229.google.com [IPv6:2607:f8b0:4003:c01::229]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 641D512D0 for ; Mon, 24 Feb 2014 17:51:14 +0000 (UTC) Received: by mail-ob0-f169.google.com with SMTP id wn1so2371885obc.28 for ; Mon, 24 Feb 2014 09:51:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=delphix.com; s=google; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=wCtQ8yt1s3bNgXTsyxWmnO7fU01wd1zFaSfGaLIq070=; b=ZFG6NlJTn0yolfM0RBHInFZ+qTkirjPJzHQrWYgw5sBFEm1ornfUxW9wXkMy3KBeR/ s4RgOeOuwK1Gjhd0RQdy5JCpROBO3AnUpUMGWHEsTWN826GYlopnJSMTfuNJiz2b9UMj xx75FJ80q7naRnizwDVD8VedU9jrYf24hK8jA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=wCtQ8yt1s3bNgXTsyxWmnO7fU01wd1zFaSfGaLIq070=; b=PBdDl0Jmqmw99shVu6vyDQysQBYUIeC3plIAaMnv80YpOyrHH2Y3P2h2brysw9AXwY QkGC4/8HRj4y1dGDeumLtj50n9kkeg9/4iCFKprNTGjQL2fg0BAJWEkjN7oYKUAW3kwf Mj7s6lfL1RWY9qmhmFjhT19Kkj+hNTV5bWvdVl2VOLO1sfxENnUQbh8EuJdEFjbSGpP5 CNPTsR6ZiakEbNhR3QNBHIM5BYQri9fm4GGz5d/PIMaLSUW53+2MdAVhG+Xy4zCJ/N0b Vj3oTh2VN7n/VXgG2vWWtG2uedclMADy2n7+0pp6Q/yiSwKdRfsIdNs2B59uNquD1kf3 lLKQ== X-Gm-Message-State: ALoCoQmRd4rIKmPl2f8FzXtgnq1J8squ6sJMdlE1SuedAB9/8X5wk9JNq9pqeQ+Enp2MwhQveXhB MIME-Version: 1.0 X-Received: by 10.60.134.200 with SMTP id pm8mr22315862oeb.40.1393264273430; Mon, 24 Feb 2014 09:51:13 -0800 (PST) Received: by 10.76.169.106 with HTTP; Mon, 24 Feb 2014 09:51:13 -0800 (PST) In-Reply-To: <20140224041454.GB2720@raichu> References: <20140224041454.GB2720@raichu> Date: Mon, 24 Feb 2014 09:51:13 -0800 Message-ID: Subject: Re: [patch] fasttrap process scratch space From: Adam Leventhal To: Mark Johnston Content-Type: text/plain; charset=ISO-8859-1 Cc: freebsd-dtrace@freebsd.org X-BeenThere: freebsd-dtrace@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: "A discussion list for developers working on DTrace in FreeBSD." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 24 Feb 2014 17:51:14 -0000 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. 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. Hope that's helpful. Adam On Sun, Feb 23, 2014 at 8:14 PM, Mark Johnston 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