繁体   English   中英

线程之间的共享内存

[英]The shared memory between threads

我使用Intel工具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: 

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

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

关于评论中的讨论,还应考虑未检测到库使用的同步机制的情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM