CCTLib
Calling-context and data-object attribution library for Intel Pin
deadspy_client.cpp File Reference
#include <stdio.h>
#include <stdlib.h>
#include "pin.H"
#include <map>
#include <unordered_map>
#include <list>
#include <inttypes.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iostream>
#include <locale>
#include <unistd.h>
#include <sys/syscall.h>
#include <assert.h>
#include <sys/mman.h>
#include <exception>
#include <sys/time.h>
#include <signal.h>
#include <string.h>
#include <setjmp.h>
#include <sstream>
#include <fstream>
#include <google/sparse_hash_map>
#include <google/dense_hash_map>
#include "cctlib.H"
+ Include dependency graph for deadspy_client.cpp:

Go to the source code of this file.

Classes

struct  DeadSpyThreadData
 
struct  DeadInfo
 
struct  MergedDeadInfo
 
struct  DeadInfoForPresentation
 

Macros

#define __STDC_FORMAT_MACROS
 
#define CONTINUOUS_DEADINFO
 
#define MAX_CCT_PRINT_DEPTH   (20)
 
#define MAX_FILE_PATH   (200)
 
#define MAX_DEAD_CONTEXTS_TO_LOG   (1000)
 
#define PAGE_OFFSET_BITS   (16LL)
 
#define PAGE_OFFSET(addr)   (addr & 0xFFFF)
 
#define PAGE_OFFSET_MASK   (0xFFFF)
 
#define PAGE_SIZE   (1 << PAGE_OFFSET_BITS)
 
#define PTR_SIZE   (sizeof(void*))
 
#define LEVEL_1_PAGE_TABLE_BITS   (20)
 
#define LEVEL_1_PAGE_TABLE_ENTRIES   (1 << LEVEL_1_PAGE_TABLE_BITS)
 
#define LEVEL_1_PAGE_TABLE_SIZE   (LEVEL_1_PAGE_TABLE_ENTRIES * PTR_SIZE)
 
#define LEVEL_2_PAGE_TABLE_BITS   (12)
 
#define LEVEL_2_PAGE_TABLE_ENTRIES   (1 << LEVEL_2_PAGE_TABLE_BITS)
 
#define LEVEL_2_PAGE_TABLE_SIZE   (LEVEL_2_PAGE_TABLE_ENTRIES * PTR_SIZE)
 
#define LEVEL_1_PAGE_TABLE_SLOT(addr)   ((((uint64_t)addr) >> (LEVEL_2_PAGE_TABLE_BITS + PAGE_OFFSET_BITS)) & 0xfffff)
 
#define LEVEL_2_PAGE_TABLE_SLOT(addr)   ((((uint64_t)addr) >> (PAGE_OFFSET_BITS)) & 0xFFF)
 
#define READ_ACTION   (0)
 
#define WRITE_ACTION   (0xff)
 
#define ONE_BYTE_READ_ACTION   (0)
 
#define TWO_BYTE_READ_ACTION   (0)
 
#define FOUR_BYTE_READ_ACTION   (0)
 
#define EIGHT_BYTE_READ_ACTION   (0)
 
#define ONE_BYTE_WRITE_ACTION   (0xff)
 
#define TWO_BYTE_WRITE_ACTION   (0xffff)
 
#define FOUR_BYTE_WRITE_ACTION   (0xffffffff)
 
#define EIGHT_BYTE_WRITE_ACTION   (0xffffffffffffffff)
 
#define CONTEXT_HASH_128BITS_TO_64BITS(curCtxt, oldCtxt, hashVar)
 
#define OLD_CTXT   (*lastIP)
 
#define DECLARE_HASHVAR(name)   uint64_t name
 
#define REPORT_DEAD(curCtxt, lastCtxt, hashVar, size)
 
#define REPORT_IF_DEAD(mask, curCtxt, lastCtxt, hashVar)
 

Functions

KNOB< UINT32 > KnobTopN (KNOB_MODE_WRITEONCE, "pintool", "d", "0", "how many top contexts to log")
 
DeadSpyThreadDataClientGetTLS (const THREADID threadId)
 
void InitThreadData (DeadSpyThreadData *const tdata)
 
bool DeadInfoComparer (const DeadInfo &first, const DeadInfo &second)
 
bool IsValidIP (DeadInfo di)
 
VOID Instruction (INS ins, VOID *v, const uint32_t opaqueHandle)
 
VOID InstructionContributionOfBBL1Byte (uint32_t count, THREADID threadId)
 
VOID InstructionContributionOfBBL2Byte (uint32_t count, THREADID threadId)
 
VOID InstructionContributionOfBBL4Byte (uint32_t count, THREADID threadId)
 
VOID InstructionContributionOfBBL8Byte (uint32_t count, THREADID threadId)
 
VOID InstructionContributionOfBBL10Byte (uint32_t count, THREADID threadId)
 
VOID InstructionContributionOfBBL16Byte (uint32_t count, THREADID threadId)
 
VOID InstructionContributionOfBBLLargeByte (uint32_t count, THREADID threadId)
 
static void InstrumentTrace (TRACE trace, void *f)
 
uint8_t * GetOrCreateShadowBaseAddress (void *address)
 
uint8_t * GetShadowBaseAddress (void *address)
 
VOID Record1ByteMemRead (VOID *addr)
 
VOID Record1ByteMemWrite (VOID *addr, const uint32_t opaqueHandle, THREADID threadId)
 
VOID Record1ByteMemWriteWithoutDead (VOID *addr, const uint32_t opaqueHandle, THREADID threadId)
 
VOID Record2ByteMemRead (VOID *addr)
 
VOID Record2ByteMemWrite (VOID *addr, const uint32_t opaqueHandle, THREADID threadId)
 
VOID Record4ByteMemRead (VOID *addr)
 
VOID Record4ByteMemWrite (VOID *addr, const uint32_t opaqueHandle, THREADID threadId)
 
VOID Record8ByteMemRead (VOID *addr)
 
VOID Record8ByteMemWrite (VOID *addr, const uint32_t opaqueHandle, THREADID threadId)
 
VOID Record10ByteMemRead (VOID *addr)
 
VOID Record10ByteMemWrite (VOID *addr, const uint32_t opaqueHandle, THREADID threadId)
 
VOID Record16ByteMemRead (VOID *addr)
 
VOID Record16ByteMemWrite (VOID *addr, const uint32_t opaqueHandle, THREADID threadId)
 
VOID RecordLargeMemRead (VOID *addr, UINT32 size)
 
VOID RecordLargeMemWrite (VOID *addr, UINT32 size, const uint32_t opaqueHandle, THREADID threadId)
 
void InspectMemRead (VOID *addr, UINT32 sz)
 
VOID Instruction (INS ins, void *v, const uint32_t opaqueHandle)
 
uint64_t GetTotalNByteWrites (uint32_t size)
 
uint64_t GetMeasurementBaseCount ()
 
void PrintEachSizeWrite ()
 
VOID Fini (INT32 code, VOID *v)
 
VOID SyscallEntry (THREADID threadIndex, CONTEXT *ctxt, SYSCALL_STANDARD std, VOID *v)
 
bool MergedDeadInfoComparer (const DeadInfoForPresentation &first, const DeadInfoForPresentation &second)
 
VOID PrintIPAndCallingContexts (const DeadInfoForPresentation &di, uint64_t measurementBaseCount)
 
VOID ImageUnload (IMG img, VOID *v)
 
void InitDeadSpy (int argc, char *argv[])
 
static INT32 Usage ()
 
static VOID ThreadStart (THREADID threadid, CONTEXT *ctxt, INT32 flags, VOID *v)
 
int main (int argc, char *argv[])
 

Variables

FILE * gTraceFile
 
std::fstream topnStream
 
static TLS_KEY client_tls_key
 
struct {
   char   dummy1 [128]
 
   string   topNLogFileName
 
   char   dummy2 [128]
 
DeadSpyGlobals
 
PIN_LOCK lock
 
uint8_t ** gL1PageTable [LEVEL_1_PAGE_TABLE_SIZE]
 
unordered_map< uint64_t, uint64_t > DeadMap
 
unordered_map< uint64_t, uint64_t >::iterator gDeadMapIt
 
uint64_t gTotalDead = 0
 
volatile uint32_t gClientNumThreads
 

Macro Definition Documentation

◆ __STDC_FORMAT_MACROS

#define __STDC_FORMAT_MACROS

Definition at line 6 of file deadspy_client.cpp.

◆ CONTEXT_HASH_128BITS_TO_64BITS

#define CONTEXT_HASH_128BITS_TO_64BITS (   curCtxt,
  oldCtxt,
  hashVar 
)
Value:
{ \
uint64_t key = (uint64_t)(oldCtxt); \
hashVar = key << 32; \
key = (uint64_t)(curCtxt); \
hashVar |= key; \
}

Definition at line 395 of file deadspy_client.cpp.

◆ CONTINUOUS_DEADINFO

#define CONTINUOUS_DEADINFO

Definition at line 50 of file deadspy_client.cpp.

◆ DECLARE_HASHVAR

#define DECLARE_HASHVAR (   name)    uint64_t name

Definition at line 412 of file deadspy_client.cpp.

◆ EIGHT_BYTE_READ_ACTION

#define EIGHT_BYTE_READ_ACTION   (0)

Definition at line 88 of file deadspy_client.cpp.

◆ EIGHT_BYTE_WRITE_ACTION

#define EIGHT_BYTE_WRITE_ACTION   (0xffffffffffffffff)

Definition at line 93 of file deadspy_client.cpp.

◆ FOUR_BYTE_READ_ACTION

#define FOUR_BYTE_READ_ACTION   (0)

Definition at line 87 of file deadspy_client.cpp.

◆ FOUR_BYTE_WRITE_ACTION

#define FOUR_BYTE_WRITE_ACTION   (0xffffffff)

Definition at line 92 of file deadspy_client.cpp.

◆ LEVEL_1_PAGE_TABLE_BITS

#define LEVEL_1_PAGE_TABLE_BITS   (20)

Definition at line 69 of file deadspy_client.cpp.

◆ LEVEL_1_PAGE_TABLE_ENTRIES

#define LEVEL_1_PAGE_TABLE_ENTRIES   (1 << LEVEL_1_PAGE_TABLE_BITS)

Definition at line 70 of file deadspy_client.cpp.

◆ LEVEL_1_PAGE_TABLE_SIZE

#define LEVEL_1_PAGE_TABLE_SIZE   (LEVEL_1_PAGE_TABLE_ENTRIES * PTR_SIZE)

Definition at line 71 of file deadspy_client.cpp.

◆ LEVEL_1_PAGE_TABLE_SLOT

#define LEVEL_1_PAGE_TABLE_SLOT (   addr)    ((((uint64_t)addr) >> (LEVEL_2_PAGE_TABLE_BITS + PAGE_OFFSET_BITS)) & 0xfffff)

Definition at line 77 of file deadspy_client.cpp.

◆ LEVEL_2_PAGE_TABLE_BITS

#define LEVEL_2_PAGE_TABLE_BITS   (12)

Definition at line 73 of file deadspy_client.cpp.

◆ LEVEL_2_PAGE_TABLE_ENTRIES

#define LEVEL_2_PAGE_TABLE_ENTRIES   (1 << LEVEL_2_PAGE_TABLE_BITS)

Definition at line 74 of file deadspy_client.cpp.

◆ LEVEL_2_PAGE_TABLE_SIZE

#define LEVEL_2_PAGE_TABLE_SIZE   (LEVEL_2_PAGE_TABLE_ENTRIES * PTR_SIZE)

Definition at line 75 of file deadspy_client.cpp.

◆ LEVEL_2_PAGE_TABLE_SLOT

#define LEVEL_2_PAGE_TABLE_SLOT (   addr)    ((((uint64_t)addr) >> (PAGE_OFFSET_BITS)) & 0xFFF)

Definition at line 78 of file deadspy_client.cpp.

◆ MAX_CCT_PRINT_DEPTH

#define MAX_CCT_PRINT_DEPTH   (20)

Definition at line 53 of file deadspy_client.cpp.

◆ MAX_DEAD_CONTEXTS_TO_LOG

#define MAX_DEAD_CONTEXTS_TO_LOG   (1000)

Definition at line 56 of file deadspy_client.cpp.

◆ MAX_FILE_PATH

#define MAX_FILE_PATH   (200)

Definition at line 54 of file deadspy_client.cpp.

◆ OLD_CTXT

#define OLD_CTXT   (*lastIP)

Definition at line 406 of file deadspy_client.cpp.

◆ ONE_BYTE_READ_ACTION

#define ONE_BYTE_READ_ACTION   (0)

Definition at line 85 of file deadspy_client.cpp.

◆ ONE_BYTE_WRITE_ACTION

#define ONE_BYTE_WRITE_ACTION   (0xff)

Definition at line 90 of file deadspy_client.cpp.

◆ PAGE_OFFSET

#define PAGE_OFFSET (   addr)    (addr & 0xFFFF)

Definition at line 61 of file deadspy_client.cpp.

◆ PAGE_OFFSET_BITS

#define PAGE_OFFSET_BITS   (16LL)

Definition at line 60 of file deadspy_client.cpp.

◆ PAGE_OFFSET_MASK

#define PAGE_OFFSET_MASK   (0xFFFF)

Definition at line 62 of file deadspy_client.cpp.

◆ PAGE_SIZE

#define PAGE_SIZE   (1 << PAGE_OFFSET_BITS)

Definition at line 65 of file deadspy_client.cpp.

◆ PTR_SIZE

#define PTR_SIZE   (sizeof(void*))

Definition at line 68 of file deadspy_client.cpp.

◆ READ_ACTION

#define READ_ACTION   (0)

Definition at line 82 of file deadspy_client.cpp.

◆ REPORT_DEAD

#define REPORT_DEAD (   curCtxt,
  lastCtxt,
  hashVar,
  size 
)
Value:
do { \
CONTEXT_HASH_128BITS_TO_64BITS(curCtxt, lastCtxt, hashVar) \
if ((gDeadMapIt = DeadMap.find(hashVar)) == DeadMap.end()) { \
DeadMap.insert(std::pair<uint64_t, uint64_t>(hashVar, size)); \
} else { \
(gDeadMapIt->second) += size; \
} \
} while (0)
unordered_map< uint64_t, uint64_t >::iterator gDeadMapIt
unordered_map< uint64_t, uint64_t > DeadMap

Definition at line 414 of file deadspy_client.cpp.

◆ REPORT_IF_DEAD

#define REPORT_IF_DEAD (   mask,
  curCtxt,
  lastCtxt,
  hashVar 
)
Value:
do { \
if (state & (mask)) { \
REPORT_DEAD(curCtxt, lastCtxt, hashVar, 1); \
} \
} while (0)

Definition at line 440 of file deadspy_client.cpp.

◆ TWO_BYTE_READ_ACTION

#define TWO_BYTE_READ_ACTION   (0)

Definition at line 86 of file deadspy_client.cpp.

◆ TWO_BYTE_WRITE_ACTION

#define TWO_BYTE_WRITE_ACTION   (0xffff)

Definition at line 91 of file deadspy_client.cpp.

◆ WRITE_ACTION

#define WRITE_ACTION   (0xff)

Definition at line 83 of file deadspy_client.cpp.

Function Documentation

◆ ClientGetTLS()

◆ DeadInfoComparer()

bool DeadInfoComparer ( const DeadInfo first,
const DeadInfo second 
)
inline

◆ Fini()

VOID Fini ( INT32  code,
VOID *  v 
)

◆ GetMeasurementBaseCount()

uint64_t GetMeasurementBaseCount ( )
inline

Definition at line 1422 of file deadspy_client.cpp.

References GetTotalNByteWrites().

Referenced by Fini(), and ImageUnload().

◆ GetOrCreateShadowBaseAddress()

◆ GetShadowBaseAddress()

◆ GetTotalNByteWrites()

◆ ImageUnload()

◆ InitDeadSpy()

void InitDeadSpy ( int  argc,
char *  argv[] 
)

Definition at line 1637 of file deadspy_client.cpp.

References gTraceFile, KnobTopN(), MAX_FILE_PATH, and topnStream.

Referenced by main().

◆ InitThreadData()

◆ InspectMemRead()

void InspectMemRead ( VOID *  addr,
UINT32  sz 
)

Definition at line 1177 of file deadspy_client.cpp.

◆ Instruction() [1/2]

VOID Instruction ( INS  ins,
VOID *  v,
const uint32_t  opaqueHandle 
)

◆ Instruction() [2/2]

◆ InstructionContributionOfBBL10Byte()

VOID InstructionContributionOfBBL10Byte ( uint32_t  count,
THREADID  threadId 
)
inline

Definition at line 248 of file deadspy_client.cpp.

References ClientGetTLS(), and DeadSpyThreadData::g16ByteWriteInstrCount.

Referenced by InstrumentTrace().

◆ InstructionContributionOfBBL16Byte()

VOID InstructionContributionOfBBL16Byte ( uint32_t  count,
THREADID  threadId 
)
inline

Definition at line 251 of file deadspy_client.cpp.

References ClientGetTLS(), and DeadSpyThreadData::g16ByteWriteInstrCount.

Referenced by InstrumentTrace().

◆ InstructionContributionOfBBL1Byte()

VOID InstructionContributionOfBBL1Byte ( uint32_t  count,
THREADID  threadId 
)
inline

Definition at line 236 of file deadspy_client.cpp.

References ClientGetTLS(), and DeadSpyThreadData::g1ByteWriteInstrCount.

Referenced by InstrumentTrace().

◆ InstructionContributionOfBBL2Byte()

VOID InstructionContributionOfBBL2Byte ( uint32_t  count,
THREADID  threadId 
)
inline

Definition at line 239 of file deadspy_client.cpp.

References ClientGetTLS(), and DeadSpyThreadData::g2ByteWriteInstrCount.

Referenced by InstrumentTrace().

◆ InstructionContributionOfBBL4Byte()

VOID InstructionContributionOfBBL4Byte ( uint32_t  count,
THREADID  threadId 
)
inline

Definition at line 242 of file deadspy_client.cpp.

References ClientGetTLS(), and DeadSpyThreadData::g4ByteWriteInstrCount.

Referenced by InstrumentTrace().

◆ InstructionContributionOfBBL8Byte()

VOID InstructionContributionOfBBL8Byte ( uint32_t  count,
THREADID  threadId 
)
inline

Definition at line 245 of file deadspy_client.cpp.

References ClientGetTLS(), and DeadSpyThreadData::g8ByteWriteInstrCount.

Referenced by InstrumentTrace().

◆ InstructionContributionOfBBLLargeByte()

VOID InstructionContributionOfBBLLargeByte ( uint32_t  count,
THREADID  threadId 
)
inline

Definition at line 254 of file deadspy_client.cpp.

References ClientGetTLS(), and DeadSpyThreadData::gLargeByteWriteInstrCount.

Referenced by InstrumentTrace().

◆ InstrumentTrace()

◆ IsValidIP()

bool IsValidIP ( DeadInfo  di)
inline

◆ KnobTopN()

KNOB<UINT32> KnobTopN ( KNOB_MODE_WRITEONCE  ,
"pintool"  ,
"d"  ,
"0"  ,
"how many top contexts to log"   
)

Referenced by Fini(), ImageUnload(), and InitDeadSpy().

◆ main()

◆ MergedDeadInfoComparer()

bool MergedDeadInfoComparer ( const DeadInfoForPresentation first,
const DeadInfoForPresentation second 
)
inline

Definition at line 1504 of file deadspy_client.cpp.

References DeadInfoForPresentation::count.

Referenced by ImageUnload().

◆ PrintEachSizeWrite()

void PrintEachSizeWrite ( )
inline

Definition at line 1429 of file deadspy_client.cpp.

References GetTotalNByteWrites(), and gTraceFile.

Referenced by ImageUnload().

◆ PrintIPAndCallingContexts()

VOID PrintIPAndCallingContexts ( const DeadInfoForPresentation di,
uint64_t  measurementBaseCount 
)
inline

◆ Record10ByteMemRead()

◆ Record10ByteMemWrite()

◆ Record16ByteMemRead()

VOID Record16ByteMemRead ( VOID *  addr)

◆ Record16ByteMemWrite()

VOID Record16ByteMemWrite ( VOID *  addr,
const uint32_t  opaqueHandle,
THREADID  threadId 
)

◆ Record1ByteMemRead()

VOID Record1ByteMemRead ( VOID *  addr)

◆ Record1ByteMemWrite()

◆ Record1ByteMemWriteWithoutDead()

VOID Record1ByteMemWriteWithoutDead ( VOID *  addr,
const uint32_t  opaqueHandle,
THREADID  threadId 
)
inline

◆ Record2ByteMemRead()

VOID Record2ByteMemRead ( VOID *  addr)

◆ Record2ByteMemWrite()

VOID Record2ByteMemWrite ( VOID *  addr,
const uint32_t  opaqueHandle,
THREADID  threadId 
)

◆ Record4ByteMemRead()

VOID Record4ByteMemRead ( VOID *  addr)

◆ Record4ByteMemWrite()

VOID Record4ByteMemWrite ( VOID *  addr,
const uint32_t  opaqueHandle,
THREADID  threadId 
)

◆ Record8ByteMemRead()

VOID Record8ByteMemRead ( VOID *  addr)

◆ Record8ByteMemWrite()

VOID Record8ByteMemWrite ( VOID *  addr,
const uint32_t  opaqueHandle,
THREADID  threadId 
)

◆ RecordLargeMemRead()

VOID RecordLargeMemRead ( VOID *  addr,
UINT32  size 
)

Definition at line 1128 of file deadspy_client.cpp.

References GetShadowBaseAddress(), ONE_BYTE_READ_ACTION, PAGE_OFFSET, and PinCCTLib::status.

Referenced by Instruction().

◆ RecordLargeMemWrite()

VOID RecordLargeMemWrite ( VOID *  addr,
UINT32  size,
const uint32_t  opaqueHandle,
THREADID  threadId 
)

Definition at line 1169 of file deadspy_client.cpp.

References Record1ByteMemWrite().

Referenced by Instruction().

◆ SyscallEntry()

VOID SyscallEntry ( THREADID  threadIndex,
CONTEXT *  ctxt,
SYSCALL_STANDARD  std,
VOID *  v 
)

Definition at line 1462 of file deadspy_client.cpp.

References Record1ByteMemRead().

Referenced by main().

◆ ThreadStart()

static VOID ThreadStart ( THREADID  threadid,
CONTEXT *  ctxt,
INT32  flags,
VOID *  v 
)
static

Definition at line 1689 of file deadspy_client.cpp.

References client_tls_key, gClientNumThreads, and InitThreadData().

Referenced by main().

◆ Usage()

static INT32 Usage ( )
static

Definition at line 1683 of file deadspy_client.cpp.

Referenced by main().

Variable Documentation

◆ client_tls_key

TLS_KEY client_tls_key
static

Definition at line 153 of file deadspy_client.cpp.

Referenced by ClientGetTLS(), main(), and ThreadStart().

◆ DeadMap

unordered_map<uint64_t, uint64_t> DeadMap

Definition at line 207 of file deadspy_client.cpp.

Referenced by ImageUnload().

◆ 

struct { ... } DeadSpyGlobals

◆ dummy1

char dummy1[128]

Definition at line 156 of file deadspy_client.cpp.

◆ dummy2

char dummy2[128]

Definition at line 158 of file deadspy_client.cpp.

◆ gClientNumThreads

volatile uint32_t gClientNumThreads

Definition at line 230 of file deadspy_client.cpp.

Referenced by GetTotalNByteWrites(), and ThreadStart().

◆ gDeadMapIt

unordered_map<uint64_t, uint64_t>::iterator gDeadMapIt

Definition at line 208 of file deadspy_client.cpp.

◆ gL1PageTable

uint8_t** gL1PageTable[LEVEL_1_PAGE_TABLE_SIZE]

Definition at line 203 of file deadspy_client.cpp.

Referenced by GetOrCreateShadowBaseAddress(), and GetShadowBaseAddress().

◆ gTotalDead

uint64_t gTotalDead = 0

Definition at line 224 of file deadspy_client.cpp.

Referenced by Fini(), and ImageUnload().

◆ gTraceFile

FILE* gTraceFile

◆ lock

PIN_LOCK lock

Definition at line 197 of file deadspy_client.cpp.

◆ topNLogFileName

string topNLogFileName

Definition at line 157 of file deadspy_client.cpp.

◆ topnStream

std::fstream topnStream

Definition at line 127 of file deadspy_client.cpp.

Referenced by Fini(), ImageUnload(), and InitDeadSpy().