| 1 | /* |
| 2 | Copyright(C) 2010-2017 Brazil |
| 3 | Copyright(C) 2020-2021 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "grn.h" |
| 21 | #include "grn_encoding.h" |
| 22 | #include "grn_windows.h" |
| 23 | |
| 24 | #ifdef WIN32 |
| 25 | # include <dbghelp.h> |
| 26 | |
| 27 | static grn_critical_section grn_windows_symbol_critical_section; |
| 28 | |
| 29 | static DWORD grn_windows_trace_logging = TLS_OUT_OF_INDEXES; |
| 30 | /* Address is important. Value is meaningless. */ |
| 31 | static bool grn_windows_trace_logging_true; |
| 32 | static bool grn_windows_trace_logging_false; |
| 33 | |
| 34 | void |
| 35 | grn_windows_init(void) |
| 36 | { |
| 37 | CRITICAL_SECTION_INIT(grn_windows_symbol_critical_section); |
| 38 | |
| 39 | grn_windows_trace_logging = TlsAlloc(); |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | grn_windows_fin(void) |
| 44 | { |
| 45 | CRITICAL_SECTION_FIN(grn_windows_symbol_critical_section); |
| 46 | |
| 47 | if (grn_windows_trace_logging != TLS_OUT_OF_INDEXES) { |
| 48 | TlsFree(grn_windows_trace_logging); |
| 49 | grn_windows_trace_logging = TLS_OUT_OF_INDEXES; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | static bool |
| 54 | grn_windows_trace_logging_get(void) |
| 55 | { |
| 56 | if (grn_windows_trace_logging == TLS_OUT_OF_INDEXES) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | return TlsGetValue(grn_windows_trace_logging) == |
| 61 | &grn_windows_trace_logging_true; |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | grn_windows_trace_logging_set(bool logging) |
| 66 | { |
| 67 | if (grn_windows_trace_logging == TLS_OUT_OF_INDEXES) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | if (logging) { |
| 72 | TlsSetValue(grn_windows_trace_logging, |
| 73 | &grn_windows_trace_logging_true); |
| 74 | } else { |
| 75 | TlsSetValue(grn_windows_trace_logging, |
| 76 | &grn_windows_trace_logging_false); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static char *windows_base_dir = NULL; |
| 81 | const char * |
| 82 | grn_windows_base_dir(void) |
| 83 | { |
| 84 | if (!windows_base_dir) { |
| 85 | HMODULE dll; |
| 86 | const wchar_t *dll_filename = GRN_DLL_FILENAME; |
| 87 | wchar_t absolute_dll_filename[MAX_PATH]; |
| 88 | DWORD absolute_dll_filename_size; |
| 89 | dll = GetModuleHandleW(dll_filename); |
| 90 | absolute_dll_filename_size = GetModuleFileNameW(dll, |
| 91 | absolute_dll_filename, |
| 92 | MAX_PATH); |
| 93 | if (absolute_dll_filename_size == 0) { |
| 94 | windows_base_dir = grn_strdup_raw("."); |
| 95 | } else { |
| 96 | DWORD ansi_dll_filename_size; |
| 97 | |
| 98 | { |
| 99 | DWORD i; |
| 100 | |
| 101 | absolute_dll_filename[absolute_dll_filename_size] = L'\0'; |
| 102 | |
| 103 | if (wcsncmp(absolute_dll_filename, L"\\\\?\\", 4) == 0) { |
| 104 | wmemmove_s(absolute_dll_filename, |
| 105 | MAX_PATH, |
| 106 | &absolute_dll_filename[4], |
| 107 | absolute_dll_filename_size - 4); |
| 108 | absolute_dll_filename[absolute_dll_filename_size - 4] = L'\0'; |
| 109 | absolute_dll_filename_size -= 4; |
| 110 | } |
| 111 | |
| 112 | for (i = 0; i < absolute_dll_filename_size; i++) { |
| 113 | if (absolute_dll_filename[i] == L'\\') { |
| 114 | absolute_dll_filename[i] = L'/'; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /* Remove basename (DLL filename). */ |
| 119 | for (i = absolute_dll_filename_size - 1; i > 0; i--) { |
| 120 | if (absolute_dll_filename[i] == L'/') { |
| 121 | absolute_dll_filename[i] = L'\0'; |
| 122 | absolute_dll_filename_size = i; |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if ((absolute_dll_filename_size >= 4) && |
| 128 | ((_wcsicmp(absolute_dll_filename + absolute_dll_filename_size - 4, |
| 129 | L"/bin") == 0) || |
| 130 | (_wcsicmp(absolute_dll_filename + absolute_dll_filename_size - 4, |
| 131 | L"/lib") == 0))) { |
| 132 | absolute_dll_filename_size -= 4; |
| 133 | absolute_dll_filename[absolute_dll_filename_size] = L'\0'; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | ansi_dll_filename_size = |
| 138 | WideCharToMultiByte(CP_ACP, 0, |
| 139 | absolute_dll_filename, absolute_dll_filename_size, |
| 140 | NULL, 0, NULL, NULL); |
| 141 | if (ansi_dll_filename_size == 0) { |
| 142 | windows_base_dir = grn_strdup_raw("."); |
| 143 | } else { |
| 144 | windows_base_dir = malloc(ansi_dll_filename_size + 1); |
| 145 | WideCharToMultiByte(CP_ACP, 0, |
| 146 | absolute_dll_filename, absolute_dll_filename_size, |
| 147 | windows_base_dir, ansi_dll_filename_size, |
| 148 | NULL, NULL); |
| 149 | windows_base_dir[ansi_dll_filename_size] = '\0'; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | return windows_base_dir; |
| 154 | } |
| 155 | |
| 156 | UINT |
| 157 | grn_windows_encoding_to_code_page(grn_encoding encoding) |
| 158 | { |
| 159 | UINT code_page; |
| 160 | |
| 161 | switch (encoding) { |
| 162 | case GRN_ENC_EUC_JP : |
| 163 | code_page = 20932; |
| 164 | break; |
| 165 | case GRN_ENC_UTF8 : |
| 166 | code_page = CP_UTF8; |
| 167 | break; |
| 168 | case GRN_ENC_SJIS : |
| 169 | code_page = 932; |
| 170 | break; |
| 171 | case GRN_ENC_LATIN1 : |
| 172 | code_page = 1252; |
| 173 | break; |
| 174 | case GRN_ENC_KOI8R : |
| 175 | code_page = 20866; |
| 176 | break; |
| 177 | default : |
| 178 | code_page = CP_ACP; |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | return code_page; |
| 183 | } |
| 184 | |
| 185 | static void |
| 186 | grn_windows_append_path(grn_ctx *ctx, |
| 187 | grn_obj *buffer, |
| 188 | const char *path) |
| 189 | { |
| 190 | for (; *path; path++) { |
| 191 | if (*path == '/') { |
| 192 | GRN_TEXT_PUTC(ctx, buffer, '\\'); |
| 193 | } else { |
| 194 | GRN_TEXT_PUTC(ctx, buffer, *path); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | static void |
| 200 | grn_windows_symbol_add_search_path(grn_ctx *ctx, |
| 201 | grn_obj *search_path, |
| 202 | const char *relative_path) |
| 203 | { |
| 204 | if (GRN_TEXT_LEN(search_path) > 0) { |
| 205 | GRN_TEXT_PUTC(ctx, search_path, ';'); |
| 206 | } |
| 207 | |
| 208 | grn_windows_append_path(ctx, search_path, grn_windows_base_dir()); |
| 209 | grn_windows_append_path(ctx, search_path, "/"); |
| 210 | grn_windows_append_path(ctx, search_path, relative_path); |
| 211 | } |
| 212 | |
| 213 | static void |
| 214 | grn_windows_symbol_add_search_paths(grn_ctx *ctx, |
| 215 | grn_obj *search_path, |
| 216 | const char *relative_path) |
| 217 | { |
| 218 | grn_obj base_dir; |
| 219 | GRN_TEXT_INIT(&base_dir, 0); |
| 220 | grn_windows_append_path(ctx, &base_dir, grn_windows_base_dir()); |
| 221 | grn_windows_append_path(ctx, &base_dir, "/"); |
| 222 | grn_windows_append_path(ctx, &base_dir, relative_path); |
| 223 | grn_windows_append_path(ctx, &base_dir, "/*"); |
| 224 | GRN_TEXT_PUTC(ctx, &base_dir, '\0'); |
| 225 | |
| 226 | WIN32_FIND_DATA data; |
| 227 | HANDLE finder = FindFirstFile(GRN_TEXT_VALUE(&base_dir), &data); |
| 228 | if (finder != INVALID_HANDLE_VALUE) { |
| 229 | do { |
| 230 | if (!(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 231 | continue; |
| 232 | } |
| 233 | if (strcmp(data.cFileName, ".") == 0) { |
| 234 | continue; |
| 235 | } |
| 236 | if (strcmp(data.cFileName, "..") == 0) { |
| 237 | continue; |
| 238 | } |
| 239 | |
| 240 | grn_obj sub_path; |
| 241 | GRN_TEXT_INIT(&sub_path, 0); |
| 242 | grn_windows_append_path(ctx, &sub_path, relative_path); |
| 243 | grn_windows_append_path(ctx, &sub_path, "/"); |
| 244 | grn_windows_append_path(ctx, &sub_path, data.cFileName); |
| 245 | GRN_TEXT_PUTC(ctx, &sub_path, '\0'); |
| 246 | grn_windows_symbol_add_search_path(ctx, |
| 247 | search_path, |
| 248 | GRN_TEXT_VALUE(&sub_path)); |
| 249 | grn_windows_symbol_add_search_paths(ctx, |
| 250 | search_path, |
| 251 | GRN_TEXT_VALUE(&sub_path)); |
| 252 | GRN_OBJ_FIN(ctx, &sub_path); |
| 253 | } while (FindNextFile(finder, &data) != 0); |
| 254 | FindClose(finder); |
| 255 | } |
| 256 | GRN_OBJ_FIN(ctx, &base_dir); |
| 257 | } |
| 258 | |
| 259 | bool |
| 260 | grn_windows_symbol_initialize(grn_ctx *ctx, HANDLE process) |
| 261 | { |
| 262 | CRITICAL_SECTION_ENTER(grn_windows_symbol_critical_section); |
| 263 | SymSetOptions(SYMOPT_ALLOW_ABSOLUTE_SYMBOLS | |
| 264 | SYMOPT_ALLOW_ZERO_ADDRESS | |
| 265 | SYMOPT_AUTO_PUBLICS | |
| 266 | SYMOPT_DEBUG | |
| 267 | SYMOPT_DEFERRED_LOADS | |
| 268 | SYMOPT_LOAD_LINES | |
| 269 | SYMOPT_NO_PROMPTS); |
| 270 | if (!SymInitialize(process, NULL, TRUE)) { |
| 271 | CRITICAL_SECTION_LEAVE(grn_windows_symbol_critical_section); |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | char current_search_path[MAX_PATH]; |
| 276 | grn_obj new_search_path; |
| 277 | GRN_TEXT_INIT(&new_search_path, 0); |
| 278 | if (SymGetSearchPath(process, |
| 279 | current_search_path, |
| 280 | sizeof(current_search_path))) { |
| 281 | GRN_TEXT_PUTS(ctx, &new_search_path, current_search_path); |
| 282 | } |
| 283 | |
| 284 | grn_windows_symbol_add_search_path(ctx, &new_search_path, "bin"); |
| 285 | grn_windows_symbol_add_search_paths(ctx, |
| 286 | &new_search_path, |
| 287 | "lib/" PACKAGE "/plugins"); |
| 288 | GRN_TEXT_PUTC(ctx, &new_search_path, '\0'); |
| 289 | |
| 290 | const BOOL success = |
| 291 | SymSetSearchPath(process, GRN_TEXT_VALUE(&new_search_path)); |
| 292 | |
| 293 | GRN_OBJ_FIN(ctx, &new_search_path); |
| 294 | |
| 295 | if (!success) { |
| 296 | SymCleanup(process); |
| 297 | CRITICAL_SECTION_LEAVE(grn_windows_symbol_critical_section); |
| 298 | } |
| 299 | |
| 300 | return success; |
| 301 | } |
| 302 | |
| 303 | bool |
| 304 | grn_windows_symbol_cleanup(grn_ctx *ctx, HANDLE process) |
| 305 | { |
| 306 | const BOOL success = SymCleanup(process); |
| 307 | CRITICAL_SECTION_LEAVE(grn_windows_symbol_critical_section); |
| 308 | return success; |
| 309 | } |
| 310 | |
| 311 | void |
| 312 | grn_windows_log_back_trace_entry(grn_ctx *ctx, |
| 313 | grn_log_level level, |
| 314 | HANDLE process, |
| 315 | DWORD64 address) |
| 316 | { |
| 317 | IMAGEHLP_MODULE64 module; |
| 318 | char *buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)]; |
| 319 | SYMBOL_INFO *symbol = (SYMBOL_INFO *)buffer; |
| 320 | DWORD line_displacement = 0; |
| 321 | IMAGEHLP_LINE64 line; |
| 322 | bool have_module_name = false; |
| 323 | bool have_symbol_name = false; |
| 324 | bool have_location = false; |
| 325 | |
| 326 | module.SizeOfStruct = sizeof(IMAGEHLP_MODULE64); |
| 327 | if (SymGetModuleInfo64(process, address, &module)) { |
| 328 | have_module_name = true; |
| 329 | } |
| 330 | |
| 331 | symbol->SizeOfStruct = sizeof(SYMBOL_INFO); |
| 332 | symbol->MaxNameLen = MAX_SYM_NAME; |
| 333 | if (SymFromAddr(process, address, NULL, symbol)) { |
| 334 | have_symbol_name = true; |
| 335 | } |
| 336 | |
| 337 | line.SizeOfStruct = sizeof(IMAGEHLP_LINE64); |
| 338 | if (SymGetLineFromAddr64(process, address, &line_displacement, &line)) { |
| 339 | have_location = true; |
| 340 | } |
| 341 | |
| 342 | { |
| 343 | const char *unknown = "(unknown)"; |
| 344 | const char *grn_encoding_image_name = unknown; |
| 345 | if (have_module_name) { |
| 346 | grn_encoding_image_name = |
| 347 | grn_encoding_convert_from_locale(ctx, module.ImageName, -1, NULL); |
| 348 | } |
| 349 | GRN_LOG(ctx, level, |
| 350 | "%s:%lu:%lu: %.*s(): <%s>: <%s>", |
| 351 | (have_location ? line.FileName : unknown), |
| 352 | (have_location ? line.LineNumber : 0), |
| 353 | (have_location ? line_displacement : 0), |
| 354 | (int)(have_symbol_name ? symbol->NameLen : strlen(unknown)), |
| 355 | (have_symbol_name ? symbol->Name : unknown), |
| 356 | (have_module_name ? module.ModuleName : unknown), |
| 357 | grn_encoding_image_name); |
| 358 | if (have_module_name) { |
| 359 | grn_encoding_converted_free(ctx, grn_encoding_image_name); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | void |
| 365 | grn_windows_log_back_trace(grn_ctx *ctx, |
| 366 | grn_log_level level, |
| 367 | HANDLE process, |
| 368 | HANDLE thread, |
| 369 | CONTEXT *context) |
| 370 | { |
| 371 | if (grn_windows_trace_logging_get()) { |
| 372 | GRN_LOG(ctx, level, "recursive trace logging isn't supported"); |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | grn_windows_trace_logging_set(true); |
| 377 | |
| 378 | if (!grn_windows_symbol_initialize(ctx, process)) { |
| 379 | GRN_LOG(ctx, level, "failed to initialize symbol handler"); |
| 380 | grn_windows_trace_logging_set(false); |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | STACKFRAME64 frame; |
| 385 | DWORD machine_type; |
| 386 | memset(&frame, 0, sizeof(STACKFRAME64)); |
| 387 | frame.AddrPC.Mode = AddrModeFlat; |
| 388 | frame.AddrReturn.Mode = AddrModeFlat; |
| 389 | frame.AddrFrame.Mode = AddrModeFlat; |
| 390 | frame.AddrStack.Mode = AddrModeFlat; |
| 391 | frame.AddrBStore.Mode = AddrModeFlat; |
| 392 | # ifdef _M_IX86 |
| 393 | machine_type = IMAGE_FILE_MACHINE_I386; |
| 394 | frame.AddrPC.Offset = context->Eip; |
| 395 | frame.AddrFrame.Offset = context->Ebp; |
| 396 | frame.AddrStack.Offset = context->Esp; |
| 397 | # elif defined(_M_IA64) /* _M_IX86 */ |
| 398 | machine_type = IMAGE_FILE_MACHINE_IA64; |
| 399 | frame.AddrPC.Offset = context->StIIP; |
| 400 | frame.AddrStack.Offset = context->IntSP; /* SP is IntSP? */ |
| 401 | frame.AddrBStore.Offset = context->RsBSP; |
| 402 | # elif defined(_M_AMD64) /* _M_IX86 */ |
| 403 | machine_type = IMAGE_FILE_MACHINE_AMD64; |
| 404 | frame.AddrPC.Offset = context->Rip; |
| 405 | frame.AddrFrame.Offset = context->Rbp; |
| 406 | frame.AddrStack.Offset = context->Rsp; |
| 407 | # elif defined(_M_ARM64) |
| 408 | machine_type = IMAGE_FILE_MACHINE_ARM64; |
| 409 | frame.AddrPC.Offset = context->Pc; |
| 410 | frame.AddrFrame.Offset = context->Fp; |
| 411 | frame.AddrStack.Offset = context->Sp; |
| 412 | # else /* _M_IX86 */ |
| 413 | # error "Intel x86, Intel Itanium, x64 and ARM64 are only supported architectures" |
| 414 | # endif /* _M_IX86 */ |
| 415 | |
| 416 | DWORD64 previous_address = 0; |
| 417 | while (true) { |
| 418 | if (!StackWalk64(machine_type, |
| 419 | process, |
| 420 | thread, |
| 421 | &frame, |
| 422 | context, |
| 423 | NULL, |
| 424 | NULL, |
| 425 | NULL, |
| 426 | NULL)) { |
| 427 | break; |
| 428 | } |
| 429 | |
| 430 | DWORD64 address = frame.AddrPC.Offset; |
| 431 | if (previous_address != 0 && address == previous_address) { |
| 432 | break; |
| 433 | } |
| 434 | |
| 435 | grn_windows_log_back_trace_entry(ctx, level, process, address); |
| 436 | previous_address = address; |
| 437 | } |
| 438 | |
| 439 | grn_windows_symbol_cleanup(ctx, process); |
| 440 | |
| 441 | grn_windows_trace_logging_set(false); |
| 442 | } |
| 443 | #endif /* WIN32 */ |