| 1 | /* |
| 2 | Copyright(C) 2015 Brazil |
| 3 | Copyright(C) 2018-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_ctx.h" |
| 21 | #include "grn_windows.h" |
| 22 | |
| 23 | #ifdef _WIN32 |
| 24 | # include <stdio.h> |
| 25 | # include <tlhelp32.h> |
| 26 | #endif |
| 27 | |
| 28 | static grn_thread_get_limit_func get_limit_func = NULL; |
| 29 | static void *get_limit_func_data = NULL; |
| 30 | static grn_thread_set_limit_func set_limit_func = NULL; |
| 31 | static void *set_limit_func_data = NULL; |
| 32 | |
| 33 | static grn_thread_get_limit_with_ctx_func get_limit_with_ctx_func = NULL; |
| 34 | static void *get_limit_with_ctx_func_data = NULL; |
| 35 | static grn_thread_set_limit_with_ctx_func set_limit_with_ctx_func = NULL; |
| 36 | static void *set_limit_with_ctx_func_data = NULL; |
| 37 | |
| 38 | uint32_t |
| 39 | grn_thread_get_limit(void) |
| 40 | { |
| 41 | if (get_limit_func) { |
| 42 | return get_limit_func(get_limit_func_data); |
| 43 | } else { |
| 44 | return 0; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | grn_thread_set_limit(uint32_t new_limit) |
| 50 | { |
| 51 | if (!set_limit_func) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | set_limit_func(new_limit, set_limit_func_data); |
| 56 | } |
| 57 | |
| 58 | uint32_t |
| 59 | grn_thread_get_limit_with_ctx(grn_ctx *ctx) |
| 60 | { |
| 61 | if (get_limit_with_ctx_func) { |
| 62 | return get_limit_with_ctx_func(ctx, get_limit_with_ctx_func_data); |
| 63 | } else { |
| 64 | return grn_thread_get_limit(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | grn_thread_set_limit_with_ctx(grn_ctx *ctx, uint32_t new_limit) |
| 70 | { |
| 71 | if (set_limit_with_ctx_func) { |
| 72 | set_limit_with_ctx_func(ctx, new_limit, set_limit_func_data); |
| 73 | } else { |
| 74 | grn_thread_set_limit(new_limit); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | grn_thread_set_get_limit_func(grn_thread_get_limit_func func, |
| 80 | void *data) |
| 81 | { |
| 82 | get_limit_func = func; |
| 83 | get_limit_func_data = data; |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | grn_thread_set_set_limit_func(grn_thread_set_limit_func func, void *data) |
| 88 | { |
| 89 | set_limit_func = func; |
| 90 | set_limit_func_data = data; |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | grn_thread_set_get_limit_with_ctx_func(grn_thread_get_limit_with_ctx_func func, |
| 95 | void *data) |
| 96 | { |
| 97 | get_limit_with_ctx_func = func; |
| 98 | get_limit_with_ctx_func_data = data; |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | grn_thread_set_set_limit_with_ctx_func(grn_thread_set_limit_with_ctx_func func, |
| 103 | void *data) |
| 104 | { |
| 105 | set_limit_with_ctx_func = func; |
| 106 | set_limit_with_ctx_func_data = data; |
| 107 | } |
| 108 | |
| 109 | grn_rc |
| 110 | grn_thread_dump(grn_ctx *ctx) |
| 111 | { |
| 112 | GRN_API_ENTER; |
| 113 | #ifdef _WIN32 |
| 114 | grn_log_level level = GRN_LOG_NOTICE; |
| 115 | HANDLE thread_snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); |
| 116 | if (thread_snapshot == INVALID_HANDLE_VALUE) { |
| 117 | SERR("[thread][dump] failed to create thread snapshot"); |
| 118 | GRN_API_RETURN(ctx->rc); |
| 119 | } |
| 120 | HANDLE process = GetCurrentProcess(); |
| 121 | DWORD process_id = GetCurrentProcessId(); |
| 122 | THREADENTRY32 entry; |
| 123 | entry.dwSize = sizeof(entry); |
| 124 | if (!Thread32First(thread_snapshot, &entry)) { |
| 125 | SERR("[thread][dump] failed to retrieve the first thread"); |
| 126 | } |
| 127 | if (ctx->rc == GRN_SUCCESS) { |
| 128 | do { |
| 129 | if (entry.th32OwnerProcessID != process_id) { |
| 130 | continue; |
| 131 | } |
| 132 | |
| 133 | HANDLE thread = OpenThread(THREAD_GET_CONTEXT, |
| 134 | FALSE, |
| 135 | entry.th32ThreadID); |
| 136 | if (thread == INVALID_HANDLE_VALUE) { |
| 137 | continue; |
| 138 | } |
| 139 | CONTEXT context; |
| 140 | context.ContextFlags = CONTEXT_FULL; |
| 141 | if (GetThreadContext(thread, &context)) { |
| 142 | GRN_LOG(ctx, level, "-- Thread %08" GRN_FMT_DWORD " --", |
| 143 | entry.th32ThreadID); |
| 144 | grn_windows_log_back_trace(ctx, |
| 145 | level, |
| 146 | process, |
| 147 | thread, |
| 148 | &context); |
| 149 | GRN_LOG(ctx, level, "---------------------"); |
| 150 | } |
| 151 | CloseHandle(thread); |
| 152 | } while (Thread32Next(thread_snapshot, &entry)); |
| 153 | } |
| 154 | CloseHandle(thread_snapshot); |
| 155 | #else |
| 156 | #endif |
| 157 | GRN_API_RETURN(ctx->rc); |
| 158 | } |