简体   繁体   English

如何在C中检测/配置内存(堆,指针)读写?

[英]How to instrument/profile memory(heap, pointers) reads and writes in C?

I know this might be a bit vague and far-fetched (sorry, stackoverflow police!). 我知道这可能有点含糊且牵强(抱歉,stackoverflow警察!)。

Is there a way, without external forces, to instrument (track basically) each pointer access and track reads and writes - either general reads/writes or quantity of reads/writes per access. 有没有办法在没有外力的情况下检测(基本上跟踪)每个指针访问并跟踪读取和写入-常规读取/写入或每次访问的读取/写入数量。 Bonus if it can be done for all variables and differentiate between stack and heap ones. 如果可以对所有变量进行操作并区分堆栈变量和堆变量,则将有好处。

Is there a way to wrap pointers in general or should this be done via custom heap? 有没有一种方法可以包装指针,还是应该通过自定义堆来完成? Even with custom heap I can't think of a way. 即使使用自定义堆,我也想不出办法。

Ultimately I'd like to see a visual representation of said logs that would show me variables represented as blocks (of bytes or multiples of) and heatmap over them for reads and writes. 最终,我希望看到上述日志的直观表示,该日志将向我显示以块(字节或字节的倍数)表示的变量,并在其上进行热图读写。

Ultra simple example: 超简单的例子:

int i = 5;
int *j = &i;

printf("%d", *j); /* Log would write *j was accessed for read and read sizeof(int) bytes

Attempt of rephrasing in more concise manner: 尝试以更简洁的方式改写:

(How) can I intercept (and log) access to a pointer in C without external instrumentation of binary? (如何)在没有外部二进制工具的情况下如何拦截(和记录)对C中指针的访问? - bonus if I can distinguish between read and write and get name of the pointer and size of read/write in bytes. -如果我可以区分读写,并获得指针的名称和读写大小(以字节为单位),则可以得到奖励。

I guess (or hope for you) that you are developing on Linux/x86-64 with a recent GCC (5.2 in october 2015) or perhaps Clang/LLVM compiler (3.7). 我猜(或希望对您有帮助)您正在使用最新的 GCC (2015年10月为5.2)或Clang / LLVM编译器(3.7)在Linux / x86-64上进行开发。

I also guess that you are tracking a naughty bug, and not asking this (too broad) question from a purely theoretical point of view. 我还猜想您正在跟踪一个顽皮的错误,而不是纯粹从理论的角度来问这个(太宽泛的)问题。

(Notice that practically there is no simple answer to your question, because in practice C compilers produce machine code close to the hardware, and most hardware do not have sophisticated instrumentations like the one you dream of) (请注意,实际上对您的问题没有简单的答案,因为在实践中,C编译器生成的机器代码与硬件接近,并且大多数硬件没有像您梦dream以求的那样复杂的工具。)

Of course, compile with all warnings and debug info ( gcc -Wall -Wextra -g ). 当然,请编译所有警告和调试信息( gcc -Wall -Wextra -g )。 Use the debugger ( gdb ), notably its watchpoint facilities which are related to your issue. 使用调试器( gdb ),尤其是与您的问题有关的监视点设施。 Use also valgrind . 也使用valgrind Notice also that GDB (recent versions like 7.10) is scriptable in Python (or Guile), and you could code some scripts for GDB to assist you. 还请注意,GDB(最新版本为7.10)可以用Python(或Guile)编写脚本,并且您可以为GDB编写一些脚本来帮助您。

Notice also that recent GCC & Clang/LLVM have several sanitizers. 还要注意,最近的GCC和Clang / LLVM有几种消毒剂。 Use some of the -fsanitize= debugging options , notably the address sanitizer with -fsanitize=address ; 使用某些-fsanitize=调试选项 ,尤其是使用-fsanitize=address地址清理器 they are instrumenting the code to help in detecting pointer accesses, so they are sort-of doing what you want. 他们正在检测代码以帮助检测指针访问,因此他们正在按照自己的意愿进行操作。 Of course, the performance of the instrumented generated code is decreasing (depending on the sanitizer, can be 10 or 20% or a factor of 50x). 当然,检测生成的代码的性能会下降(取决于消毒剂,可能是10%或20%或50倍)。

At last, you might even consider adding your own instrumentation by customizing your compiler , eg with MELT -a high level domain specific language designed for such customization tasks for GCC. 最后,您甚至可以考虑通过自定义编译器来添加自己的工具,例如使用MELT-一种针对GCC的自定义任务而设计的高级领域特定语言。 This would take months of work, unless you are already familiar with GCC internals (then, only several weeks). 除非您已经熟悉GCC的内部知识(然后只有几个星期),否则这将需要数月的工作。 You could add an "optimization" pass inside GCC which would instrument (by changing the Gimple code) whatever accesses or stores you want. 您可以在GCC内添加一个“优化”过程,该过程将通过更改Gimple代码来检测(访问)或存储所需的内容。

Read more about aspect-oriented programming . 阅读有关面向方面编程的更多信息。

Notice also that if your C code is generated, that is if you are meta-programming , then changing the C code generator might be very relevant. 还请注意,如果生成了C代码(即正在元编程中) ,那么更改C代码生成器可能非常相关。 Read more about reflection and homoiconicity . 阅读有关反射谐音的更多信息。 Dynamic software updating is also related to your issues. 动态软件更新也与您的问题有关。

Look also into profiling tools like oprofile and into sound static source analyzers like Frama-C . 还可以查看诸如oprofile之类的分析工具以及诸如Frama-C之类的声音静态源分析器

You could also run your program inside some (instrumenting) emulator (like Qemu , Unisim , etc...). 您也可以在一些(仪表化) 模拟器 (例如QemuUnisim等)中运行程序。

You might also compile for a fictitious architecture like MMIX and instrument its emulator. 您可能还为诸如MMIX的虚拟架构进行了编译,并为其仿真器进行了测试。

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

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