我是一个大型商业程序的开发人员,我正试图追踪一个特定的 C++ 内存泄漏。我想使用 Visual Studio 搜索我的进程的整个有效地址空间,但我看不出如何有效地执行此搜索。
我知道 .s 命令文档 here ,但它没有做我需要的。例如,我 100% 确定地址 0xfdfd240 包含值 0x0f0e34a8。我可以像这样成功地使用 .s 命令在该地址附近进行搜索:
.s -d 0x0fdfd200 L256000000 0x0f0e34a8
Found match at
0xfdfd240
但是我的程序进行了许多小的分配,这给我留下了许多小的非连续内存部分。如果我备份几千字节,搜索命令将失败:
.s -d 0x0fd00000 L256000000 0x0f0e34a8
Memory location could not be read. Please specify a valid memory location.
而且当搜索命令发现它的第一个错误地址时,它似乎不再尝试向前搜索,即使在它之外还有有效地址。
.s -d 0x0f000000 L256000000 0x0f0e34a8
No match was found
我隐约知道有一种方法可以询问 Windows 哪些内存范围对给定进程有效,因此我正在考虑编写一个小的一次性程序来收集该信息,然后自动执行一系列搜索命令立即窗口...但似乎有人以前处理过这个问题,并且一定做了更聪明的事情。
此外,我可以提取正在运行的进程的转储文件,因此如果有人可以推荐第三方工具将转储提供给具有更强大搜索功能的第三方工具,那么应该也能解决问题。
有什么建议吗?
编辑:我在 VS2008SP1 和 VS2010SP1 中看到了这种行为。
最佳答案
这是一个小工具,可以在目标进程中找到内存块1,在 block 中搜索模式,并打印出找到模式的地址。现在它相当原始——纯命令行,希望您提供目标进程的 PID,只接受命令行上的模式作为单个字符串。搜索部分应该相当健壮,所以这主要是一个非常弱的 UI 问题——尽管如此,只要您要查找的模式是您可以在命令行上输入字符串形式的内容,它就可以正常工作。如果你想包含不可打印的字符,很容易在 translator 中添加我多年前写过,可以让您在命令行上使用 C 风格的转义序列。
1 跳过包含该进程的可执行文件/DLL 代码等内容的 block 。
#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
#include <algorithm>
#include <iterator>
template <class outIter>
void find_locs(HANDLE process, std::string const &pattern, outIter output) {
unsigned char *p = NULL;
MEMORY_BASIC_INFORMATION info;
// VirtualQueryEx does the hard part, telling use about each
// block in the target process.
for ( p = NULL;
VirtualQueryEx(process, p, &info, sizeof(info)) == sizeof(info);
p += info.RegionSize )
{
// buffer to hold copies of memory blocks from the target
std::vector<char> buffer;
std::vector<char>::iterator pos;
// We only want committed memory that's mapped or private --
// screens out things like the code in the target process.
//
if (info.State == MEM_COMMIT &&
(info.Type == MEM_MAPPED || info.Type == MEM_PRIVATE))
{
DWORD bytes_read;
// copy block from target to our buffer, search for pattern:
buffer.resize(info.RegionSize);
ReadProcessMemory(process, p, &buffer[0], info.RegionSize, &bytes_read);
buffer.resize(bytes_read);
// find all occurrences of pattern in buffer:
for ( pos = buffer.begin();
buffer.end()!=(pos=std::search(pos, buffer.end(), pattern.begin(), pattern.end()));
++pos)
{
// record each address in target where pattern was found:
*output++ = p+(pos-buffer.begin());
}
}
}
}
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <process ID> <pattern>", argv[0]);
return 1;
}
// Read the target PID and search pattern:
int pid;
sscanf(argv[1], "%i", &pid);
std::string pattern(argv[2]);
// Open the target process with rights to read its information and memory:
HANDLE process = OpenProcess(
PROCESS_VM_READ | PROCESS_QUERY_INFORMATION,
false,
pid);
// Find the locations, and write them to standard output, one per line:
find_locs(process, pattern,
std::ostream_iterator<void *>(std::cout, "\n"));
return 0;
}
关于c++ - 如何从 Visual Studio 调试器执行所有进程内存的高级搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5494340/