Search code examples
valgrindinstrumentation

Valgrind examine memory, patching lackey


I would like to patch valgrind's lackey example tool. I would like to examine the memory of the instrumented binary for the appearence of a certain string sequence around the pointer of a store instruction. Alternatively scan all memory regions on each store for the appearence of such a sequence. Does anyone know a reference to a adequate example? Basically I'd like to

for (i = -8; i <= 8; i++) {
   if (strncmp(ptr+i, "needle", 6) == 0) 
      printf("Here ip: %x\n", ip);
}

But how can I verify that ptr in the range of [-8,8] is valid? Is there a function that tracks the heap regions? Or do I have to track /proc/pid/maps each time?

// Konrad


Solution

  • Turns out that the exp-dhat tools in valgrind works for me:

    static VG_REGPARM(3)
      void dh_handle_write ( Addr addr, UWord szB )
    {
       Block* bk = find_Block_containing(addr);
       if (bk) {
    
         if (is_subinterval_of(bk->payload, bk->req_szB, addr-10, 10*2)) {
           int i = 0;
           for (i = -10; i <= 10; i++) {
             if ((VG_(memcmp)(((char*)addr)+ i, searchfor, 6) == 0)) {
    
               ExeContext *ec = VG_(record_ExeContext)( VG_(get_running_tid)(), 0 );
               VG_(pp_ExeContext) ( ec );
               VG_(printf)(" ---------------- -----------  found %08lx  @ %08lx --------\n", addr, ip);
             }
           }
         }
    
    
          bk->n_writes += szB;
          if (bk->histoW)
             inc_histo_for_block(bk, addr, szB);
       }
    }
    

    Each time for a write I search for the occurance of array searchfor and print a stacktrace if found...