线程

时间:2015-11-27 07:17:11

标签: c++ c multithreading memory intel-pin

我使用英特尔工具Pin检测多线程进程并监视Linux上线程之间的共享内存访问,我在Pin开发了一个工具来记录共享内存地址,检测Pin中的代码如下:

VOID Instruction(INS ins, VOID *v)
{
    UINT32 memOperands = INS_MemoryOperandCount(ins);

    // Iterate over each memory operand of the instruction.
    for (UINT32 memOp = 0; memOp < memOperands; memOp++)
    {
        if (INS_MemoryOperandIsRead(ins, memOp))
        {
            INS_InsertPredicatedCall(
                ins, IPOINT_BEFORE, (AFUNPTR)RecordMemRead,
                IARG_INST_PTR,
                IARG_MEMORYOP_EA, memOp,
                IARG_END);
        }
        // Note that in some architectures a single memory operand can be
        // both read and written (for instance incl (%eax) on IA-32)
        // In that case we instrument it once for read and once for write.
        if (INS_MemoryOperandIsWritten(ins, memOp))
        {
            INS_InsertPredicatedCall(
                ins, IPOINT_BEFORE, (AFUNPTR)RecordMemWrite,
                IARG_INST_PTR,
                IARG_MEMORYOP_EA, memOp,
                IARG_END);
        }
    }
}

函数RecordMemReadRecordMemWrite用于在读或写内存时记录线程的信息和内存地址,并且我在此函数中使用了锁。
我想记录线程之间共享的内存地址,例如全局变量或堆内存 但是当我使用一个简单的多线程程序来测试我的工具时。测试如下。在此程序中,用户尚未定义任何共享变量或共享内存:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>

void * fun1(void *arg)
{

}

int main(int argc,char* argv[])
{
    pthread_t npid1;

    pthread_create(&npid1,NULL,fun1,NULL);

    pthread_join(npid1,NULL);

    return 0;
}

结果表示多线程访问的内存,并在下一行输出内存访问指令的调试信息:

read addr: b775252c
line:0 col: 0 file: 
write addr: b775252c
line:0 col: 0 file: 
write addr: b775252c
line:0 col: 0 file: 
write addr: b775252c
line:0 col: 0 file: 
write addr: b775252c
line:0 col: 0 file: 
write addr: b775252c
line:0 col: 0 file: 
write addr: b775252c
line:0 col: 0 file: 
read addr: b556ad64
line:0 col: 0 file: 
read addr: b556abc4
line:0 col: 0 file: 
write addr: b556abc4
line:0 col: 0 file: 

结果表明两个线程都有一些内存访问,而读/写指令没有调试信息(我在编译中添加-g选项),所以这些内存可能是由库访问的Q1:这些内存有什么线程?
Q2:如果我只想监视用户定义的内存,未在库中定义,如何区分它们?

1 个答案:

答案 0 :(得分:0)

  1. 显然,您需要有关这些地址中发生的更多信息。我建议使用pin的RTN_ *和IMG API来获取有关正在执行的例程的更多信息,然后检查反汇编程序中的相关图像。
  2. 您可以按代码源过滤访问,而不是按内存过滤。只需避免使用作为系统库一部分的指令。
  3. 关于评论中的讨论,还应考虑您未检测到库使用的同步机制的情况。