xref: /dpdk/lib/eal/windows/eal_debug.c (revision ae67895b507bb6af22263c79ba0d5c374b396485)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2019 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #include <stdarg.h>
699a2dd95SBruce Richardson #include <rte_log.h>
799a2dd95SBruce Richardson #include <rte_debug.h>
899a2dd95SBruce Richardson #include <rte_windows.h>
999a2dd95SBruce Richardson 
10*ae67895bSDavid Marchand #include "eal_private.h"
11*ae67895bSDavid Marchand 
1299a2dd95SBruce Richardson #include <dbghelp.h>
1399a2dd95SBruce Richardson 
1499a2dd95SBruce Richardson #define BACKTRACE_SIZE 256
1599a2dd95SBruce Richardson 
1699a2dd95SBruce Richardson /* dump the stack of the calling core */
1799a2dd95SBruce Richardson void
rte_dump_stack(void)1899a2dd95SBruce Richardson rte_dump_stack(void)
1999a2dd95SBruce Richardson {
2099a2dd95SBruce Richardson 	PVOID stack_trace[BACKTRACE_SIZE] = {0};
2199a2dd95SBruce Richardson 	USHORT frame_num;
2299a2dd95SBruce Richardson 	BOOL ret;
2399a2dd95SBruce Richardson 	HANDLE process = GetCurrentProcess();
2499a2dd95SBruce Richardson 
2599a2dd95SBruce Richardson 	ret = SymInitialize(process, NULL, TRUE);
2699a2dd95SBruce Richardson 	if (!ret) {
2799a2dd95SBruce Richardson 		RTE_LOG_WIN32_ERR("SymInitialize()");
2899a2dd95SBruce Richardson 		return;
2999a2dd95SBruce Richardson 	}
3099a2dd95SBruce Richardson 
3199a2dd95SBruce Richardson 	SymSetOptions(SYMOPT_LOAD_LINES | SYMOPT_UNDNAME);
3299a2dd95SBruce Richardson 
3399a2dd95SBruce Richardson 	frame_num = RtlCaptureStackBackTrace(0, BACKTRACE_SIZE,
3499a2dd95SBruce Richardson 			stack_trace, NULL);
3599a2dd95SBruce Richardson 
3699a2dd95SBruce Richardson 	while (frame_num > 0) {
3799a2dd95SBruce Richardson 		DWORD64 address = (DWORD64)(stack_trace[frame_num - 1]);
3899a2dd95SBruce Richardson 		DWORD64 sym_disp = 0;
3999a2dd95SBruce Richardson 		DWORD error_code = 0, lin_disp;
4099a2dd95SBruce Richardson 		char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
4199a2dd95SBruce Richardson 		PSYMBOL_INFO symbol_info = (PSYMBOL_INFO)buffer;
4299a2dd95SBruce Richardson 		IMAGEHLP_LINE64 line;
4399a2dd95SBruce Richardson 
4499a2dd95SBruce Richardson 		symbol_info->SizeOfStruct = sizeof(SYMBOL_INFO);
4599a2dd95SBruce Richardson 		symbol_info->MaxNameLen = MAX_SYM_NAME;
4699a2dd95SBruce Richardson 		line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
4799a2dd95SBruce Richardson 
4899a2dd95SBruce Richardson 		ret = SymFromAddr(process, address, &sym_disp, symbol_info);
4999a2dd95SBruce Richardson 		if (!ret) {
5099a2dd95SBruce Richardson 			error_code = GetLastError();
5199a2dd95SBruce Richardson 			if (error_code == ERROR_INVALID_ADDRESS) {
5299a2dd95SBruce Richardson 				/* Missing symbols, print message */
53*ae67895bSDavid Marchand 				EAL_LOG(ERR,
54*ae67895bSDavid Marchand 				    "%d: [<missing_symbols>]", frame_num--);
5599a2dd95SBruce Richardson 				continue;
5699a2dd95SBruce Richardson 			} else {
5799a2dd95SBruce Richardson 				RTE_LOG_WIN32_ERR("SymFromAddr()");
5899a2dd95SBruce Richardson 				goto end;
5999a2dd95SBruce Richardson 			}
6099a2dd95SBruce Richardson 		}
6199a2dd95SBruce Richardson 
6299a2dd95SBruce Richardson 		ret = SymGetLineFromAddr64(process, address, &lin_disp, &line);
6399a2dd95SBruce Richardson 		if (!ret) {
6499a2dd95SBruce Richardson 			error_code = GetLastError();
6599a2dd95SBruce Richardson 			/* If ERROR_INVALID_ADDRESS tag unknown and proceed */
6699a2dd95SBruce Richardson 			if (error_code != ERROR_INVALID_ADDRESS) {
6799a2dd95SBruce Richardson 				RTE_LOG_WIN32_ERR("SymGetLineFromAddr64()");
6899a2dd95SBruce Richardson 				goto end;
6999a2dd95SBruce Richardson 			}
7099a2dd95SBruce Richardson 		}
7199a2dd95SBruce Richardson 
72*ae67895bSDavid Marchand 		EAL_LOG(ERR,
73*ae67895bSDavid Marchand 			"%d: [%s (%s+0x%0llx)[0x%0llX]]", frame_num,
7499a2dd95SBruce Richardson 			error_code ? "<unknown>" : line.FileName,
7599a2dd95SBruce Richardson 			symbol_info->Name, sym_disp, symbol_info->Address);
7699a2dd95SBruce Richardson 		frame_num--;
7799a2dd95SBruce Richardson 	}
7899a2dd95SBruce Richardson end:
7999a2dd95SBruce Richardson 	ret = SymCleanup(process);
8099a2dd95SBruce Richardson 	if (!ret)
8199a2dd95SBruce Richardson 		RTE_LOG_WIN32_ERR("SymCleanup()");
8299a2dd95SBruce Richardson }
83