Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何通过DbgHelp获取局部变量的值_C++_Visual Studio_Winapi_Visual Studio 2013_Dbghelp - Fatal编程技术网

C++ 如何通过DbgHelp获取局部变量的值

C++ 如何通过DbgHelp获取局部变量的值,c++,visual-studio,winapi,visual-studio-2013,dbghelp,C++,Visual Studio,Winapi,Visual Studio 2013,Dbghelp,如何通过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 enu

如何通过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';
  }
}
#包括
#包括
#包括
#包括
#包括
#包括
#pragma注释(lib,“Dbghelp.lib”)
BOOL回调枚举符号回调(PSYMBOL\u INFO pSymInfo、ULONG SymbolSize、PVOID UserContext)
{
如果(符号化==0)
{
返回TRUE;
}
auto*local\u variables\u info=reinterpret\u cast(用户上下文);
std::unique_ptr bytes_read(新的无符号字符[SymbolSize]);
HANDLE cur_process_HANDLE=GetCurrentProcess();
实际读取的字节数的大小;
if(ReadProcessMemory(
电流处理
,重新解释(pSymInfo->Address)
,bytes_read.get()
,符号化
,&实际读取的字节数!=0)
{
unsigned char*bytes=bytes_read.get();
局部变量\u info->operator[](pSymInfo->Name)=std::to_字符串(pSymInfo->Value);
}
返回TRUE;//继续枚举
}
std::map get_local_variables_info()
{
映射本地变量信息;
HANDLE cur_process_HANDLE=GetCurrentProcess();
if(symminitialize(cur\u process\u handle,NULL,TRUE)=FALSE)
{
返回本地变量信息;
}
提升\u范围\u退出\u全部(当前进程\u句柄)
{
SymCleanup(当前进程句柄);
};
const ULONG frames_至_skip=0;
const ULONG frames_至_capture=1;
void*堆栈[帧到帧捕获];
const USHORT frames=capturestacktrace(
帧\u至\u跳过
,帧到帧捕获
堆栈
无效的
);
如果(帧!=1)
{
返回本地变量信息;
}
图像HLP_堆栈_帧sf;
sf.InstructionOffset=重新解释强制转换(堆栈[0]);
if(SymSetContext)(
电流处理,
&sf,//上下文
0//未使用
)==假)
{
返回本地变量信息;
}
if(符号)符号(
电流处理,
0,//0->SymEnumSymbols将使用带有SymSetContext的上下文集
0,//掩码也必须为0才能使用上下文
枚举符号回调,
&本地变量信息//用户定义上下文
)==假)
{
返回本地变量信息;
}
返回本地变量信息;
}
int main()
{
int-foo=0;
const auto&local_variables_info=get_local_variables_info();
for(const std::pair e:local_variables_info)
{

std::cout您要查找的变量值的地址位于函数开始的地址(可从执行上下文的EBP寄存器访问),从堆栈中函数的该变量位置的偏移量应用。 在您的情况下(函数的局部变量),pSymInfo->Address成员存储变量相对于堆栈帧的偏移量

(这里有一个解释:)

因此,您要查找的地址位于(EBP+pSymInfo->address)

要启动函数,可以使用:

  • 上下文->EBP寄存器
  • StackWalk64()->允许遍历调用堆栈
  • GetThreadContext()->以获取上下文

  • 谢谢你的回答!为什么下面的代码说
    foo
    的值为0?