Index: helgrind/hg_main.c =================================================================== --- helgrind/hg_main.c (revision 6479) +++ helgrind/hg_main.c (working copy) @@ -1773,6 +1773,14 @@ static +void hg_new_mem_mmap( Addr a, SizeT len, Off64T offset, Bool rr, Bool ww, Bool xx ) +{ + /* Ignore the permissions, just make it readable. Seems to work... */ + make_segment_readable(a, len); +} + + +static void hg_new_mem_heap ( Addr a, SizeT len, Bool is_inited ) { if (is_inited) { @@ -3437,7 +3445,7 @@ /* stack ones not decided until hg_post_clo_init() */ VG_(track_new_mem_brk) (& make_writable); - VG_(track_new_mem_mmap) (& hg_new_mem_startup); + VG_(track_new_mem_mmap) (& hg_new_mem_mmap); VG_(track_change_mem_mprotect) (& hg_set_perms); Index: memcheck/mc_main.c =================================================================== --- memcheck/mc_main.c (revision 6479) +++ memcheck/mc_main.c (working copy) @@ -2539,7 +2539,7 @@ } static -void mc_new_mem_mmap ( Addr a, SizeT len, Bool rr, Bool ww, Bool xx ) +void mc_new_mem_mmap ( Addr a, SizeT len, Off64T o, Bool rr, Bool ww, Bool xx ) { MC_(make_mem_defined)(a, len); } Index: include/pub_tool_tooliface.h =================================================================== --- include/pub_tool_tooliface.h (revision 6479) +++ include/pub_tool_tooliface.h (working copy) @@ -467,7 +467,7 @@ Bool rr, Bool ww, Bool xx)); void VG_(track_new_mem_stack_signal)(void(*f)(Addr a, SizeT len)); void VG_(track_new_mem_brk) (void(*f)(Addr a, SizeT len)); -void VG_(track_new_mem_mmap) (void(*f)(Addr a, SizeT len, +void VG_(track_new_mem_mmap) (void(*f)(Addr a, SizeT len, Off64T offset, Bool rr, Bool ww, Bool xx)); void VG_(track_copy_mem_remap) (void(*f)(Addr from, Addr to, SizeT len)); Index: configure.in =================================================================== --- configure.in (revision 6479) +++ configure.in (working copy) @@ -924,6 +924,9 @@ lackey/Makefile lackey/tests/Makefile lackey/docs/Makefile + mmt/Makefile + mmt/docs/Makefile + mmt/tests/Makefile none/Makefile none/tests/Makefile none/tests/amd64/Makefile Index: mmt/tests/Makefile.am =================================================================== Index: mmt/mmt_main.c =================================================================== --- mmt/mmt_main.c (revision 0) +++ mmt/mmt_main.c (revision 0) @@ -0,0 +1,289 @@ +/* + * vim:sw=3 ts=3 sts=3 noexpandtab + */ + +/*--------------------------------------------------------------------*/ +/*--- mmaptrace: The mmaptracer tool. mmt_main.c ---*/ +/*--------------------------------------------------------------------*/ + +/* + Copyright (C) 2006 Dave Airlie + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307, USA. + + The GNU General Public License is contained in the file COPYING. +*/ + +#include "pub_tool_basics.h" +#include "pub_tool_libcprint.h" +#include "pub_tool_libcassert.h" +#include "pub_tool_tooliface.h" +#include "pub_tool_debuginfo.h" +#include "pub_tool_libcbase.h" +#include "pub_tool_options.h" +#include "pub_tool_machine.h" // VG_(fnptr_to_fnentry) + +#define MAX_ITEMS 100 + +static struct mmt_memmap { + Bool initialized; + Addr addr; + SizeT len; +} mmt_gpu_reg[MAX_ITEMS]; + +static UInt current_item; + +/* Command line options */ +UInt mmt_clo_offset = (UInt) -1; + +static struct mmt_memmap *find_mmap(Addr addr) +{ + struct mmt_memmap *ptr = NULL; + UInt i; + + for (i = 0; i < MAX_ITEMS; i++) { + ptr = &mmt_gpu_reg[i]; + + if (!ptr->initialized) { + /* since we cannot un-init entries in the array, + * we can return once we hit the first uninitialized + * item -- there can't be initialized items following it. + */ + return NULL; + } + + if ((addr > ptr->addr) && (addr < ptr->addr + ptr->len)) + return ptr; + } + + return NULL; +} + +static VG_REGPARM(2) void +trace_store(Addr addr, SizeT size, UInt value) +{ + struct mmt_memmap *ptr; + + ptr = find_mmap(addr); + if (!ptr) + return; + + VG_(printf)("store: %p, %d, %p\n", addr - ptr->addr, size, value); +} + +static VG_REGPARM(2) void +trace_load(Addr addr, SizeT size) +{ + struct mmt_memmap *ptr; + UInt value; + + ptr = find_mmap(addr); + if (!ptr) + return; + + switch (size) { + case 1: + value = *(UChar*) addr; + break; + case 2: + value = *(UShort*) addr; + break; + case 4: + value = *(UInt*) addr; + break; + default: + return; + } + + VG_(printf)("load: %p, %d, %p\n", addr - ptr->addr, size, value); +} + +static void +handle_load(IRSB* bb, IRExpr* addr, Int size) +{ + IRExpr** argv = mkIRExprVec_2(addr, mkIRExpr_HWord(size)); + IRDirty* di = unsafeIRDirty_0_N(2, + "trace_load", + VG_(fnptr_to_fnentry)(trace_load), + argv); + addStmtToIRSB(bb, IRStmt_Dirty(di)); +} + +static void +handle_store(IRSB*bb, IRExpr* addr, Int size, IRExpr* data) +{ + IRExpr** argv = mkIRExprVec_3(addr, mkIRExpr_HWord(size), data); + IRDirty* di = unsafeIRDirty_0_N(2, + "trace_store", + VG_(fnptr_to_fnentry)(trace_store), + argv); + addStmtToIRSB(bb, IRStmt_Dirty(di)); +} + +static IRSB* +mmt_instrument(VgCallbackClosure* closure, + IRSB* bbIn, + VexGuestLayout* layout, + VexGuestExtents* vge, + IRType gWordTy, IRType hWordTy) +{ + IRSB* bbOut; + int i = 0; + + if (gWordTy != hWordTy) { + /* We don't currently support this case. */ + VG_(tool_panic)("host/guest word size mismatch"); + } + + /* Set up BB */ + bbOut = deepCopyIRSBExceptStmts(bbIn); + + /* Copy verbatim any IR preamble preceding the first IMark */ + while (i < bbIn->stmts_used && bbIn->stmts[i]->tag != Ist_IMark) { + addStmtToIRSB(bbOut, bbIn->stmts[i]); + i++; + } + + for (; i < bbIn->stmts_used; i++) { + IRStmt* st = bbIn->stmts[i]; + IRExpr* data_expr; + IRType arg_ty; + IRTemp t = IRTemp_INVALID; + IRStmt* cast = NULL; + + if (!st) + continue; + + if (st->tag == Ist_Store) { + arg_ty = typeOfIRExpr(bbIn->tyenv, st->Ist.Store.data); + + data_expr = st->Ist.Store.data; + + /* convert data_expr into a 32 bit value */ + switch (arg_ty) { + case Ity_I8: + t = newIRTemp(bbOut->tyenv, Ity_I32); + cast = IRStmt_WrTmp(t, IRExpr_Unop(Iop_8Uto32, data_expr)); + break; + case Ity_I16: + t = newIRTemp(bbOut->tyenv, Ity_I32); + cast = IRStmt_WrTmp(t, IRExpr_Unop(Iop_16Uto32, data_expr)); + break; + case Ity_I32: + /* nothing to do */ + break; + default: + /* since we only support 8/16/32 bit stores, we will + * never have to evaluate the data_expr that this case + * generates. + * + * this is just to pacify valgrind, since we cannot pass + * eg Ity_F64's to trace_store. + */ + t = newIRTemp(bbOut->tyenv, Ity_I32); + cast = IRStmt_WrTmp(t, IRExpr_Const (IRConst_U32 (0))); + break; + } + + if (cast) { + addStmtToIRSB(bbOut, cast); + data_expr = IRExpr_RdTmp(t); + } + + handle_store(bbOut, st->Ist.Store.addr, + sizeofIRType(arg_ty), data_expr); + } else if (st->tag == Ist_WrTmp) { + data_expr = st->Ist.WrTmp.data; + + if (data_expr->tag == Iex_Load) { + handle_load(bbOut, data_expr->Iex.Load.addr, + sizeofIRType (data_expr->Iex.Load.ty)); + } + } + + addStmtToIRSB(bbOut, st); + } + + return bbOut; +} + +static Bool +mmt_process_cmd_line_option(Char* arg) +{ + /* FIXME: make this argument mandatory */ + VG_BNUM_CLO(arg, "--offset", mmt_clo_offset, 0, 0xffffffff) + else + return False; + + return True; +} + +static void +mmt_print_usage(void) +{ + VG_(printf)(" --offset=\n"); +} + +static void +mmt_print_debug_usage(void) +{ +} + +static void +mmt_new_mem_mmap(Addr a, SizeT len, Off64T offset, Bool rr, Bool ww, Bool xx) +{ + if (offset != mmt_clo_offset) + return; + + mmt_gpu_reg[current_item].addr = a; + mmt_gpu_reg[current_item].len = len; + mmt_gpu_reg[current_item].initialized = True; + current_item++; + + VG_(message)(Vg_UserMsg, "got new mmap for %p, len %d, offset %p\n", a, len, offset); +} + +static void +mmt_fini(Int exitcode) +{ +} + +static void mmt_post_clo_init(void) +{ +} + +static void +mmt_pre_clo_init(void) +{ + VG_(details_name) ("mmaptrace"); + VG_(details_version) (NULL); + VG_(details_description) ("an MMAP tracer"); + VG_(details_copyright_author)( + "Copyright (C) 2006, and GNU GPL'd, by Dave Airlie."); + VG_(details_bug_reports_to) (VG_BUGS_TO); + + VG_(basic_tool_funcs) (mmt_post_clo_init, + mmt_instrument, + mmt_fini); + + VG_(needs_command_line_options)(mmt_process_cmd_line_option, + mmt_print_usage, + mmt_print_debug_usage); + + VG_(track_new_mem_mmap) (mmt_new_mem_mmap); +} + +VG_DETERMINE_INTERFACE_VERSION(mmt_pre_clo_init) Index: mmt/docs/Makefile.am =================================================================== Index: mmt/Makefile.am =================================================================== --- mmt/Makefile.am (revision 0) +++ mmt/Makefile.am (revision 0) @@ -0,0 +1,47 @@ +include $(top_srcdir)/Makefile.tool.am + +noinst_PROGRAMS = +if VGP_X86_LINUX +noinst_PROGRAMS += mmt-x86-linux +endif +if VGP_AMD64_LINUX +noinst_PROGRAMS += mmt-amd64-linux +endif +if VGP_PPC32_LINUX +noinst_PROGRAMS += mmt-ppc32-linux +endif +if VGP_PPC64_LINUX +noinst_PROGRAMS += mmt-ppc64-linux +endif + +MMT_SOURCES_COMMON = mmt_main.c + +mmt_x86_linux_SOURCES = $(MMT_SOURCES_COMMON) +mmt_x86_linux_CPPFLAGS = $(AM_CPPFLAGS_X86_LINUX) +mmt_x86_linux_CFLAGS = $(AM_CFLAGS_X86_LINUX) +mmt_x86_linux_DEPENDENCIES = $(COREGRIND_LIBS_X86_LINUX) +mmt_x86_linux_LDADD = $(TOOL_LDADD_X86_LINUX) +mmt_x86_linux_LDFLAGS = $(TOOL_LDFLAGS_X86_LINUX) + +mmt_amd64_linux_SOURCES = $(MMT_SOURCES_COMMON) +mmt_amd64_linux_CPPFLAGS = $(AM_CPPFLAGS_AMD64_LINUX) +mmt_amd64_linux_CFLAGS = $(AM_CFLAGS_AMD64_LINUX) +mmt_amd64_linux_DEPENDENCIES = $(COREGRIND_LIBS_AMD64_LINUX) +mmt_amd64_linux_LDADD = $(TOOL_LDADD_AMD64_LINUX) +mmt_amd64_linux_LDFLAGS = $(TOOL_LDFLAGS_AMD64_LINUX) + +mmt_ppc32_linux_SOURCES = $(MMT_SOURCES_COMMON) +mmt_ppc32_linux_CPPFLAGS = $(AM_CPPFLAGS_PPC32_LINUX) +mmt_ppc32_linux_CFLAGS = $(AM_CFLAGS_PPC32_LINUX) +mmt_ppc32_linux_DEPENDENCIES = $(COREGRIND_LIBS_PPC32_LINUX) +mmt_ppc32_linux_LDADD = $(TOOL_LDADD_PPC32_LINUX) +mmt_ppc32_linux_LDFLAGS = $(TOOL_LDFLAGS_PPC32_LINUX) + +mmt_ppc64_linux_SOURCES = $(MMT_SOURCES_COMMON) +mmt_ppc64_linux_CPPFLAGS = $(AM_CPPFLAGS_PPC64_LINUX) +mmt_ppc64_linux_CFLAGS = $(AM_CFLAGS_PPC64_LINUX) +mmt_ppc64_linux_DEPENDENCIES = $(COREGRIND_LIBS_PPC64_LINUX) +mmt_ppc64_linux_LDADD = $(TOOL_LDADD_PPC64_LINUX) +mmt_ppc64_linux_LDFLAGS = $(TOOL_LDFLAGS_PPC64_LINUX) + +mmtincludedir = $(includedir)/valgrind Index: Makefile.am =================================================================== --- Makefile.am (revision 6479) +++ Makefile.am (working copy) @@ -8,6 +8,7 @@ callgrind \ massif \ lackey \ + mmt \ none # Temporary: we want to compile Helgrind, but not regtest it. Index: coregrind/m_syswrap/syswrap-generic.c =================================================================== --- coregrind/m_syswrap/syswrap-generic.c (revision 6479) +++ coregrind/m_syswrap/syswrap-generic.c (working copy) @@ -159,7 +159,7 @@ ww = toBool(prot & VKI_PROT_WRITE); xx = toBool(prot & VKI_PROT_EXEC); - VG_TRACK( new_mem_mmap, a, len, rr, ww, xx ); + VG_TRACK( new_mem_mmap, a, len, offset, rr, ww, xx ); if (d) VG_(discard_translations)( (Addr64)a, (ULong)len, @@ -302,7 +302,7 @@ VG_TRACK( copy_mem_remap, old_addr, new_addr, MIN_SIZET(old_len,new_len) ); if (new_len > old_len) - VG_TRACK( new_mem_mmap, new_addr+old_len, new_len-old_len, + VG_TRACK( new_mem_mmap, new_addr+old_len, new_len-old_len, 0, old_seg->hasR, old_seg->hasW, old_seg->hasX ); VG_TRACK(die_mem_munmap, old_addr, old_len); if (d) { @@ -344,7 +344,7 @@ if (ok && advised == needA) { ok = VG_(am_extend_map_client)( &d, (NSegment*)old_seg, needL ); if (ok) { - VG_TRACK( new_mem_mmap, needA, needL, + VG_TRACK( new_mem_mmap, needA, needL, 0, old_seg->hasR, old_seg->hasW, old_seg->hasX ); if (d) @@ -365,7 +365,7 @@ VG_TRACK( copy_mem_remap, old_addr, advised, MIN_SIZET(old_len,new_len) ); if (new_len > old_len) - VG_TRACK( new_mem_mmap, advised+old_len, new_len-old_len, + VG_TRACK( new_mem_mmap, advised+old_len, new_len-old_len, 0, old_seg->hasR, old_seg->hasW, old_seg->hasX ); VG_TRACK(die_mem_munmap, old_addr, old_len); if (d) { @@ -404,7 +404,7 @@ ok = VG_(am_extend_map_client)( &d, (NSegment*)old_seg, needL ); if (!ok) goto eNOMEM; - VG_TRACK( new_mem_mmap, needA, needL, + VG_TRACK( new_mem_mmap, needA, needL, 0, old_seg->hasR, old_seg->hasW, old_seg->hasX ); if (d) VG_(discard_translations)( needA, needL, "do_remap(6)" ); @@ -1693,7 +1693,7 @@ /* we don't distinguish whether it's read-only or * read-write -- it doesn't matter really. */ - VG_TRACK( new_mem_mmap, res, segmentSize, True, True, False ); + VG_TRACK( new_mem_mmap, res, segmentSize, 0, True, True, False ); if (d) VG_(discard_translations)( (Addr64)res, (ULong)VG_PGROUNDUP(segmentSize), Index: coregrind/m_tooliface.c =================================================================== --- coregrind/m_tooliface.c (revision 6479) +++ coregrind/m_tooliface.c (working copy) @@ -279,7 +279,7 @@ DEF(track_new_mem_startup, Addr, SizeT, Bool, Bool, Bool) DEF(track_new_mem_stack_signal, Addr, SizeT) DEF(track_new_mem_brk, Addr, SizeT) -DEF(track_new_mem_mmap, Addr, SizeT, Bool, Bool, Bool) +DEF(track_new_mem_mmap, Addr, SizeT, Off64T, Bool, Bool, Bool) DEF(track_copy_mem_remap, Addr, Addr, SizeT) DEF(track_change_mem_mprotect, Addr, SizeT, Bool, Bool, Bool) Index: coregrind/pub_core_tooliface.h =================================================================== --- coregrind/pub_core_tooliface.h (revision 6479) +++ coregrind/pub_core_tooliface.h (working copy) @@ -159,7 +159,7 @@ void (*track_new_mem_startup) (Addr, SizeT, Bool, Bool, Bool); void (*track_new_mem_stack_signal)(Addr, SizeT); void (*track_new_mem_brk) (Addr, SizeT); - void (*track_new_mem_mmap) (Addr, SizeT, Bool, Bool, Bool); + void (*track_new_mem_mmap) (Addr, SizeT, Off64T, Bool, Bool, Bool); void (*track_copy_mem_remap) (Addr src, Addr dst, SizeT); void (*track_change_mem_mprotect) (Addr, SizeT, Bool, Bool, Bool);