看到這個,覺得實在應該要記錄一下我的困惑:MSDN » IMAGEHLP_SYMBOL64 Structure

Windows 的Debug Help Library (dbghelp.dll),是用來幫助我們撰寫 debugger,或針對 PE (Portable Executable) 執行檔做一些特殊處理。IMAGEHLP_SYMBOL64dbghelp.dll 提供的一個結構,搭配 SymGetSymFromAddr64(),可以讓我們從任意的程式執行位址,如當下的 espebp 暫存器的值,取得含括這個位址的 function symbol[1]。白話一點講,就是給定一個位址,得到這個位址位在哪一個函式裡面。再搭配 StackWalk64(),我們就可以取得程式的 backtrace[2]

不過,查看 IMAGEHLP_SYMBOL64 的 MSDN 頁面,卻看到讓我感到十分困惑的說明:

...

Members

...

MaxNameLength
The maximum length of the string that the Name member can contain, in characters, not including the null-terminating character. Because symbol names can vary in length, this data structure is allocated by the caller. This member is used so the library knows how much memory is available for use by the symbol name.
Name
The decorated or undecorated symbol name. If the buffer is not large enough for the complete name, it is truncated to MaxNameLength characters, including the null-terminating character.

這是怎樣?一下子 null-terminating character 包含在裡面,一下子又不包含在裡面。


  1. 後綴的 64,代表可相容於 64-bit 環境。
  2. 限於 debug 模式裡。