- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何通过 DbgHelp 获取局部变量的值?我尝试使用以下代码
#include <boost/scope_exit.hpp>
#include <Windows.h>
#include <Dbghelp.h>
#include <iostream>
#include <map>
#include <string>
#pragma comment(lib, "Dbghelp.lib")
BOOL CALLBACK enum_symbols_callback(PSYMBOL_INFO pSymInfo, ULONG SymbolSize, PVOID UserContext)
{
if (SymbolSize == 0)
{
return TRUE;
}
auto* local_variables_info = reinterpret_cast<std::map<std::string, std::string>*>(UserContext);
std::unique_ptr<unsigned char[]> bytes_read(new unsigned char[SymbolSize]);
HANDLE cur_process_handle = GetCurrentProcess();
SIZE_T number_of_bytes_actually_read;
if (ReadProcessMemory(
cur_process_handle
, reinterpret_cast<void*>(pSymInfo->Address)
, bytes_read.get()
, SymbolSize
, &number_of_bytes_actually_read) != 0)
{
unsigned char* bytes = bytes_read.get();
local_variables_info->operator[](pSymInfo->Name) = std::to_string(pSymInfo->Value);
}
return TRUE; // Continue enumeration
}
std::map<std::string, std::string> get_local_variables_info()
{
std::map<std::string, std::string> local_variables_info;
HANDLE cur_process_handle = GetCurrentProcess();
if (SymInitialize(cur_process_handle, NULL, TRUE) == FALSE)
{
return local_variables_info;
}
BOOST_SCOPE_EXIT_ALL(cur_process_handle)
{
SymCleanup(cur_process_handle);
};
const ULONG frames_to_skip = 0;
const ULONG frames_to_capture = 1;
void* stack[frames_to_capture];
const USHORT frames = CaptureStackBackTrace(
frames_to_skip
, frames_to_capture
, stack
, NULL
);
if (frames != 1)
{
return local_variables_info;
}
IMAGEHLP_STACK_FRAME sf;
sf.InstructionOffset = reinterpret_cast<DWORD_PTR>(stack[0]);
if (SymSetContext(
cur_process_handle,
&sf, // The context
0 // Not used
) == FALSE)
{
return local_variables_info;
}
if (SymEnumSymbols(
cur_process_handle,
0, // 0 -> SymEnumSymbols will use the context set with SymSetContext
0, // Mask must also be 0 to use the context
enum_symbols_callback,
&local_variables_info // User-defined context
) == FALSE)
{
return local_variables_info;
}
return local_variables_info;
}
int main()
{
int foo = 0;
const auto& local_variables_info = get_local_variables_info();
for (const std::pair<std::string, std::string> e : local_variables_info)
{
std::cout << e.first << ' ' << e.second << '\n';
}
}
但是reinterpret_cast<void*>(pSymInfo->Address)
总是返回类似 FFFFFFD4 的地址。
为什么?我究竟做错了什么?我该如何解决?
提前致谢。
最佳答案
您正在寻找的变量值的地址位于函数的开始地址(可从执行上下文的 EBP 寄存器访问),从堆栈中函数的此变量位置的偏移量应用。在您的情况下(函数的局部变量),pSymInfo->Address 成员存储变量相对于堆栈帧的偏移量。
(这里有一个解释:http://eli.thegreenplace.net/2011/02/04/where-the-top-of-the-stack-is-on-x86/)
所以你要找的地址在 (EBP + pSymInfo->Address)
要启动函数,您可以使用:
关于c++ - 如何通过 DbgHelp 获取局部变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26998377/
我最近实现了一个实用程序来使用 MiniDumpWriteDump 编写用户模式转储( Debug Help Library 的一部分)。实现被分成它自己的进程。目标进程可以启动此实用程序并与其通信以
我正在构建一个 Symbols 服务,我希望能够打印结构变量及其偏移量。我正在加载所需的 pdb,并试图找到适合给定掩码的结构。例如,我想获取有关结构 _RTL_CRITICAL_SECTION 的详
我的应用程序依赖于 DBGHELP.DLL 函数,尤其是有关目标进程加载的 DLL 的信息。然而,很多时候它在低于 Vista 的 Windows 版本上失败(你知道 XP 仍然存在!)。环顾四周,发
如何通过 DbgHelp 获取局部变量的值?我尝试使用以下代码 #include #include #include #include #include #include #pragma
如果您包括 #include 例如,在控制台 C++ 应用程序中,它不会从头文件中获取任何定义,并且或多或少会出现类似的编译器错误,就好像根本不包含此头文件一样。 编译器错误如下: 1>------
我想打印函数名(导入的、导出的、普通/本地函数)而不是变量名等等。 SymEnumSymbolsEx枚举所有符号,但我只想要函数。里面也找不到怎么区分函数和变量callback功能。 有没有办法只枚举
我正在使用 SymEnumSymbols获取给定掩码的所有匹配符号,并使用 CALLBACK 函数将它们推送到 vector 中。问题是,符号名称(在 PSYMBOL_INFO 结构中)只是函数的名称
我从 C# 代码调用 SymGetModuleInfo64 的行为非常奇怪。我总是使用 Marshal.GetLastWin32Error() 得到 ERROR_INVALID_PARAMETER (
我的应用程序使用 VC6 sp6 编译,使用 DBGHelp.lib 获取堆栈信息。代码在调试和发布编译中是相似的。问题在于,在执行调试版本时,DBGHelp.dll 会加载 msvcrt.dll(发
简洁明了: DIA(调试接口(interface)访问)和 DBGHELP 库之间的实际区别是什么?我知道 DIA 是基于 COM 的并且似乎非常强大(并且能够与托管和非托管进程一起使用),而 DBG
一年多以前,我已经使用 WinDbg 和 DebugDiag 来查找我们在 Java 中使用的 JNI native DLL 中的内存泄漏。现在我正在寻找线程句柄泄漏。我使用 Process Expl
我正在努力通过 Minidump (DbgHelp.h) 获取句柄信息。 我正在从映射文件流中获取 MINIDUMP_HANDLE_DESCRIPTOR_2 列表。然后,我通过使用 ObjectInf
如何在不使用 dbghelp.dll 的情况下在 Windows 上获取地址堆栈跟踪? 我不需要知道与地址关联的符号或函数名是什么,我只需要地址列表 -- 类似于 backtrace of *nix
我使用 Jochen Kalmbach's StackWalker class from CodeProject , 在我的 DLL 中发生异常时生成堆栈跟踪。 依赖于DbgHelp.dll DbgH
所有,我尝试在我的 MFC 项目中使用 Visual Leak Detector,当我尝试调试程序时,我得到了这个错误,这是什么? 干杯 岱岳 最佳答案 这是一个 DLL Hell 问题。此函数仅存在
我正在尝试使用 StackWalk64 在 x64 进程上遍历包含托管帧和 native 帧的调用堆栈。在第一个或第二个托管框架之前一切正常,之后 StackWalk64 无法找出框架的返回地址并失败
当我从 "Playing for Data" 论文构建 renderdoc 项目时,出现以下错误(作者对 VS13 没有问题,所以我想知道它是否可能是 VS15 问题): 1>------ Build
我是一名优秀的程序员,十分优秀!