[英]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);
}
}
}
函数RecordMemRead
和RecordMemWrite
用于在读取或写入内存时记录线程的信息和内存地址,我在此函数中使用了锁。
我想记录线程之间共享的内存地址,例如全局变量或堆内存。
但是当我使用一个简单的多线程程序来测试我的工具时。 测试如下。 在此程序中,用户尚未定义任何共享变量或共享内存:
#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:如果我只想监视用户定义的内存,而不是库中定义的内存,如何区分它们?
关于评论中的讨论,还应考虑未检测到库使用的同步机制的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.