找到一种方法来检测特定C代码在运行期间正在使用哪个内存位置 [英] finding a way to detect which memory location a particular C- code is using during the run time

查看:78
本文介绍了找到一种方法来检测特定C代码在运行期间正在使用哪个内存位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有什么办法可以实际访问或读取任何c程序分配的内存地址.我想做一个示例项目,编写C代码来检测内存错误或C程序在运行期间正在访问哪些内存位置.
类似于Linux上的Valgrind的工具.
通过C实现此特定功能的任何建议或概念.

问候,

Hi,
Is there any way that i can actually access or read the memory address allocated by any c program. I want to do a sample project in writing a C code to detect memory bugs or what memory locations a c program is accessing during the run time.
Similar tool like Valgrind on linux.
Any suggestions or concepts to implement this particular feature through C.

Regards,

推荐答案

大多数常用工具使用的基本方法是替换所有直接操作内存地址的函数,即i. e. alloc,malloc,calloc,realloc,free,new,delete,也许还包括memset,memmove等功能.您可以通过用宏替换所有这些功能来做到这一点,但是更安全的方法是定义一个new_handler(请参见 http://www.cplusplus.com/reference/std/new/set_new_handler/ [ ^ ]),但这样做不会影响已编译的库您链接到您的程序或在运行时加载的dll.这个概念是,每次分配内存时,您都会分配一些额外的内存,以将统计信息和数据保存在单独的位置,也可以保存在同一内存块中.然后,每次释放内存时,都使用该附加数据来验证给定地址处的内存块是否确实已分配并且尚未释放.

也就是说,大多数编译器已经实现了现有功能,至少在调试模式下进行了编译.您必须查看编译器文档以了解更多信息.例如如果用于在调试模式下运行程序,Visual Studio将在程序的正常出口处打印未正确释放的内存块列表,以及分配它们的源文件位置.可用)

同样,即使当前使用的实际物理内存已经被分割成较小的位,所有现代OS都通过能够模拟大量连续内存的专用内存管理器来分配内存.所有内存访问都必须通过该内存管理器才能确定物理地址,因此,如果您对程序使用的实际物理地址感兴趣,将很难确定.但是,只要您不打算分析来自其他进程的其他程序的内存位置,这将无关紧要.
The basic methods most common tools use is replace all functions that directly manipulate memory addresses, i. e. alloc, malloc, calloc, realloc, free, new, delete, and maybe also functions like memset, memmove, etc.. You could do this by replacing all of these functions with macros, but a safer way would be defining a new_handler (see http://www.cplusplus.com/reference/std/new/set_new_handler/[^]), but doing so will not affect compiled libs you link to your program or dlls loaded at runtime. The concept is, everytime you allocate memory, you allocate some additional memory to save statistics and data for analysis, either in a separate place, or right in the same memory block. Then, everytime you free memory, you use that additional data to verify the memory block at the given address was indeed allocated and hasn''t been freed already.

That said, most compilers implement the existing functions already just like that, at least when you compile them in debug mode. You''ll have to check the compiler documentation to find out more. E. g. Visual Studio, if used to run a program in debug mode, will at the normal exit of the program print a list of memory blocks that weren''t properly freed, along with the source file locations they were allocated in. (if that information is available)

Also, all modern OSes allocate memory through specialiced memory managers that are capable of emulating a huge chunk of contiguous memory, even if the actual, physical memory currently in use is already fragmented to smaller bits. All memory accesses have to pass through that memory manager in order to determine the physical address, so if you are interested in the actual physical addresses your program uses, it will be a lot harder to determine. This will not be relevant however as long as you don''t intend to analyze memory locations of other programs from a different process.


这篇关于找到一种方法来检测特定C代码在运行期间正在使用哪个内存位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆