简体   繁体   English

跟踪在Linux上加载到内存中的映像的内存地址

[英]Tracing memory address for an image loaded into memory on linux

I am trying to trace information everytime an image on linux is loaded into memory. 每当Linux上的映像加载到内存时,我都试图跟踪信息。 Ideally, I would need 理想情况下,我需要

  • the pid of the process, 过程的pid
  • the timestamp of the event, 事件的时间戳,
  • path from where the image is being loaded, 加载图像的路径,
  • and also the location in memory where the image gets loaded. 以及图像在内存中的加载位置。

I have managed to get the pid, timestamp and path using the open_exec tracepoint available on linux, but I'm having problems tracing the memory location where the image is loaded. 我已经使用Linux上可用的open_exec跟踪点设法获取了pid,时间戳和路径,但是在跟踪加载映像的内存位置时遇到了问题。 Any suggestions (tracepoints,markers, syscalls, etc) how I could trace this ? 任何建议(跟踪点,标记,系统调用等)如何跟踪?

One idea could be to trace all mmap(2) calls, in a manner similar to how strace(1) traces calls (srace uses ptrace(2) APIs). 一种想法可能是以类似于strace(1)跟踪调用的方式跟踪所有mmap(2)调用(srace使用ptrace(2)API)。 You can try yourself by checking out what strace seen by using strace (some command) 2> FOO and then grep mmap FOO : 您可以通过使用strace (some command) 2> FOO然后使用grep mmap FOO来查看看到的strace (some command) 2> FOO来尝试一下:

mmap2(NULL, 40654, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb80e5000
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb80e4000
mmap2(0x724000, 37456, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x724000
mmap2(0x72c000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x7) = 0x72c000
mmap2(0x27e000, 117704, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x27e000
mmap2(0x299000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1a) = 0x299000
mmap2(0x697000, 17008, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x697000
mmap2(0x69b000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x3) = 0x69b000

You have the corresponding file name of each descriptor (from open(2)), and the return value of mmap(2) will be the address where the image is mapped in memory. 您具有每个描述符的相应文件名(来自open(2)),mmap(2)的返回值将是映像在内存中的映射地址。

Depending on how you can intercept the process, you can also inject a hook on dlopen, though the above should be enough 根据上面的方法,您也可以在dlopen上插入一个钩子,尽管上面的内容就足够了

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

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