StackWalk2 function (dbghelp.h)

Obtains a stack trace.

Syntax

BOOL IMAGEAPI StackWalk2(
  [in]           DWORD                            MachineType,
  [in]           HANDLE                           hProcess,
  [in]           HANDLE                           hThread,
  [in, out]      LPSTACKFRAME_EX                  StackFrame,
  [in, out]      PVOID                            ContextRecord,
  [in, optional] PREAD_PROCESS_MEMORY_ROUTINE64   ReadMemoryRoutine,
  [in, optional] PFUNCTION_TABLE_ACCESS_ROUTINE64 FunctionTableAccessRoutine,
  [in, optional] PGET_MODULE_BASE_ROUTINE64       GetModuleBaseRoutine,
  [in, optional] PTRANSLATE_ADDRESS_ROUTINE64     TranslateAddress,
  [in, optional] PGET_TARGET_ATTRIBUTE_VALUE64    GetTargetAttributeValue,
  [in]           DWORD                            Flags
);

Parameters

[in] MachineType

The architecture type of the computer for which the stack trace is generated. This parameter can be one of the following values.

Value Meaning
IMAGE_FILE_MACHINE_I386
0x014c
Intel x86
IMAGE_FILE_MACHINE_IA64
0x0200
Intel Itanium
IMAGE_FILE_MACHINE_AMD64
0x8664
x64 (AMD64 or EM64T)
IMAGE_FILE_MACHINE_ARM64
0xaa64
ARM64

[in] hProcess

A handle to the process for which the stack trace is generated. If the caller supplies a valid callback pointer for the ReadMemoryRoutine parameter, then this value does not have to be a valid process handle. It can be a token that is unique and consistently the same for all calls to the StackWalk2 function. If the symbol handler is used with StackWalk2, use the same process handles for the calls to each function.

[in] hThread

A handle to the thread for which the stack trace is generated. If the caller supplies a valid callback pointer for the ReadMemoryRoutine parameter, then this value does not have to be a valid thread handle. It can be a token that is unique and consistently the same for all calls to the StackWalk2 function.

[in, out] StackFrame

A pointer to a STACKFRAME_EX structure. This structure receives information for the next frame, if the function call succeeds.

[in, out] ContextRecord

A pointer to an ARM64_NT_CONTEXT structure. This parameter is required only when the MachineType parameter is not IMAGE_FILE_MACHINE_I386. However, it is recommended that this parameter contain a valid context record. This allows StackWalk2 to handle a greater variety of situations.

This context may be modified, so do not pass a context record that should not be modified.

[in, optional] ReadMemoryRoutine

A callback routine that provides memory read services. When the StackWalk2 function needs to read memory from the process's address space, the ReadProcessMemoryProc64 callback function is used.

If this parameter is NULL, then the function uses a default routine. In this case, the hProcess parameter must be a valid process handle.

If this parameter is not NULL, the application should implement and register a symbol handler callback function that handles CBA_READ_MEMORY.

[in, optional] FunctionTableAccessRoutine

A callback routine that provides access to the run-time function table for the process. This parameter is required because the StackWalk2 function does not have access to the process's run-time function table. For more information, see FunctionTableAccessProc64 callback function.

The symbol handler provides functions that load and access the run-time table. If these functions are used, then the SymFunctionTableAccess64 function can be passed as a valid parameter.

[in, optional] GetModuleBaseRoutine

A callback routine that provides a module base for any given virtual address. This parameter is required. For more information, see PGET_MODULE_BASE_ROUTINE64 callback function.

The symbol handler provides functions that load and maintain module information. If these functions are used, then the SymGetModuleBase64 function can be passed as a valid parameter.

[in, optional] TranslateAddress

A callback routine that provides address translation for 16-bit addresses. For more information, see PTRANSLATE_ADDRESS_ROUTINE64 callback function.

Most callers of StackWalk2 can safely pass NULL for this parameter.

[in, optional] GetTargetAttributeValue

A callback routine that provides the values of target attributes required to walk the stack. For more information, see PGET_TARGET_ATTRIBUTE_VALUE64 callback function.

Many callers of StackWalk2 can safely pass NULL for this parameter. Callers on ARM64 platforms which may utilize pointer authentication should provide a callback.

[in] Flags

A combination of zero or more flags.

SYM_STKWALK_DEFAULT (0)

SYM_STKWALK_FORCE_FRAMEPTR (1)

Return value

If the function succeeds, the return value is TRUE.

If the function fails, the return value is FALSE. Note that StackWalk2 generally does not set the last error code.

Remarks

The StackWalk2 function provides a portable method for obtaining a stack trace. Using the StackWalk2 function is recommended over writing your own function because of all the complexities associated with stack walking on platforms. In addition, there are compiler options that cause the stack to appear differently, depending on how the module is compiled. By using this function, your application has a portable stack trace that continues to work as the compiler and operating system change.

The first call to this function will fail if the AddrPC, AddrFrame, and AddrStack members of the STACKFRAME64 structure passed in the StackFrame parameter are not initialized.

All DbgHelp functions, such as this one, are single threaded. Therefore, calls from more than one thread to this function will likely result in unexpected behavior or memory corruption. To avoid this, you must synchronize all concurrent calls from more than one thread to this function.

Requirements

Requirement Value
Target Platform Windows
Header dbghelp.h
Library DbgHelp.lib
DLL DbgHelp.dll
Redistributable DbgHelp.dll 10.0.22621.4602 or later

See also

ARM64_NT_CONTEXT structure

DbgHelp Functions

FunctionTableAccessProc64 callback function

ReadProcessMemoryProc64 callback function

STACKFRAME_EX structure

SymFunctionTableAccess64 function

SymGetModuleBase64 function

PTRANSLATE_ADDRESS_ROUTINE64 callback function

PGET_TARGET_ATTRIBUTE_VALUE64 callback function