jit_debug.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (C) 2015 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * Copyright (C) 2021 Ant Group. All rights reserved.
  17. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  18. */
  19. #include "bh_log.h"
  20. #include "bh_platform.h"
  21. #include "../../interpreter/wasm_runtime.h"
  22. #include <stdio.h>
  23. #include <assert.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include <stdbool.h>
  28. /* This must be kept in sync with gdb/gdb/jit.h */
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /* clang-format off */
  33. typedef enum JITAction {
  34. JIT_NOACTION = 0,
  35. JIT_REGISTER_FN,
  36. JIT_UNREGISTER_FN
  37. } JITAction;
  38. /* clang-format on */
  39. typedef struct JITCodeEntry {
  40. struct JITCodeEntry *next_;
  41. struct JITCodeEntry *prev_;
  42. const uint8 *symfile_addr_;
  43. uint64 symfile_size_;
  44. } JITCodeEntry;
  45. typedef struct JITDescriptor {
  46. uint32 version_;
  47. uint32 action_flag_;
  48. JITCodeEntry *relevant_entry_;
  49. JITCodeEntry *first_entry_;
  50. } JITDescriptor;
  51. #if defined(_WIN32) || defined(_WIN32_)
  52. #define attribute_noinline __declspec(noinline)
  53. #else
  54. #define attribute_noinline __attribute__((noinline))
  55. #endif
  56. /* LLVM has already define this */
  57. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  58. /**
  59. * GDB will place breakpoint into this function.
  60. * To prevent GCC from inlining or removing it we place noinline attribute
  61. * and inline assembler statement inside.
  62. */
  63. void attribute_noinline
  64. __jit_debug_register_code(void);
  65. void attribute_noinline
  66. __jit_debug_register_code(void)
  67. {
  68. int x;
  69. *(char *)&x = '\0';
  70. }
  71. /**
  72. * GDB will inspect contents of this descriptor.
  73. * Static initialization is necessary to prevent GDB from seeing
  74. * uninitialized descriptor.
  75. */
  76. JITDescriptor __jit_debug_descriptor = { 1, JIT_NOACTION, NULL, NULL };
  77. #else
  78. extern void
  79. __jit_debug_register_code();
  80. extern JITDescriptor __jit_debug_descriptor;
  81. #endif
  82. /**
  83. * Call __jit_debug_register_code indirectly via global variable.
  84. * This gives the debugger an easy way to inject custom code to
  85. * handle the events.
  86. */
  87. void (*__jit_debug_register_code_ptr)(void) = __jit_debug_register_code;
  88. #ifdef __cplusplus
  89. }
  90. #endif
  91. typedef struct WASMJITDebugEngine {
  92. korp_mutex jit_entry_lock;
  93. bh_list jit_entry_list;
  94. } WASMJITDebugEngine;
  95. typedef struct WASMJITEntryNode {
  96. struct WASMJITEntryNode *next;
  97. JITCodeEntry *entry;
  98. } WASMJITEntryNode;
  99. static WASMJITDebugEngine *jit_debug_engine;
  100. static JITCodeEntry *
  101. CreateJITCodeEntryInternal(const uint8 *symfile_addr, uint64 symfile_size)
  102. {
  103. JITCodeEntry *entry;
  104. os_mutex_lock(&jit_debug_engine->jit_entry_lock);
  105. if (!(entry = wasm_runtime_malloc(sizeof(JITCodeEntry)))) {
  106. LOG_ERROR("WASM JIT Debug Engine error: failed to allocate memory");
  107. os_mutex_unlock(&jit_debug_engine->jit_entry_lock);
  108. return NULL;
  109. }
  110. entry->symfile_addr_ = symfile_addr;
  111. entry->symfile_size_ = symfile_size;
  112. entry->prev_ = NULL;
  113. entry->next_ = __jit_debug_descriptor.first_entry_;
  114. if (entry->next_ != NULL) {
  115. entry->next_->prev_ = entry;
  116. }
  117. __jit_debug_descriptor.first_entry_ = entry;
  118. __jit_debug_descriptor.relevant_entry_ = entry;
  119. __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN;
  120. (*__jit_debug_register_code_ptr)();
  121. os_mutex_unlock(&jit_debug_engine->jit_entry_lock);
  122. return entry;
  123. }
  124. static void
  125. DestroyJITCodeEntryInternal(JITCodeEntry *entry)
  126. {
  127. os_mutex_lock(&jit_debug_engine->jit_entry_lock);
  128. if (entry->prev_ != NULL) {
  129. entry->prev_->next_ = entry->next_;
  130. }
  131. else {
  132. __jit_debug_descriptor.first_entry_ = entry->next_;
  133. }
  134. if (entry->next_ != NULL) {
  135. entry->next_->prev_ = entry->prev_;
  136. }
  137. __jit_debug_descriptor.relevant_entry_ = entry;
  138. __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN;
  139. (*__jit_debug_register_code_ptr)();
  140. wasm_runtime_free(entry);
  141. os_mutex_unlock(&jit_debug_engine->jit_entry_lock);
  142. }
  143. bool
  144. jit_debug_engine_init(void)
  145. {
  146. if (jit_debug_engine) {
  147. return true;
  148. }
  149. if (!(jit_debug_engine = wasm_runtime_malloc(sizeof(WASMJITDebugEngine)))) {
  150. LOG_ERROR("WASM JIT Debug Engine error: failed to allocate memory");
  151. return false;
  152. }
  153. memset(jit_debug_engine, 0, sizeof(WASMJITDebugEngine));
  154. if (os_mutex_init(&jit_debug_engine->jit_entry_lock) != 0) {
  155. wasm_runtime_free(jit_debug_engine);
  156. jit_debug_engine = NULL;
  157. return false;
  158. }
  159. bh_list_init(&jit_debug_engine->jit_entry_list);
  160. return true;
  161. }
  162. void
  163. jit_debug_engine_destroy(void)
  164. {
  165. if (jit_debug_engine) {
  166. WASMJITEntryNode *node, *node_next;
  167. /* Destroy all nodes */
  168. node = bh_list_first_elem(&jit_debug_engine->jit_entry_list);
  169. while (node) {
  170. node_next = bh_list_elem_next(node);
  171. DestroyJITCodeEntryInternal(node->entry);
  172. bh_list_remove(&jit_debug_engine->jit_entry_list, node);
  173. wasm_runtime_free(node);
  174. node = node_next;
  175. }
  176. /* Destroy JIT Debug Engine */
  177. os_mutex_destroy(&jit_debug_engine->jit_entry_lock);
  178. wasm_runtime_free(jit_debug_engine);
  179. jit_debug_engine = NULL;
  180. }
  181. }
  182. bool
  183. jit_code_entry_create(const uint8 *symfile_addr, uint64 symfile_size)
  184. {
  185. JITCodeEntry *entry;
  186. WASMJITEntryNode *node;
  187. if (!(node = wasm_runtime_malloc(sizeof(WASMJITEntryNode)))) {
  188. LOG_ERROR("WASM JIT Debug Engine error: failed to allocate memory");
  189. return false;
  190. }
  191. entry = CreateJITCodeEntryInternal(symfile_addr, symfile_size);
  192. if (!entry) {
  193. wasm_runtime_free(node);
  194. return false;
  195. }
  196. node->entry = entry;
  197. os_mutex_lock(&jit_debug_engine->jit_entry_lock);
  198. bh_list_insert(&jit_debug_engine->jit_entry_list, node);
  199. os_mutex_unlock(&jit_debug_engine->jit_entry_lock);
  200. return true;
  201. }
  202. void
  203. jit_code_entry_destroy(const uint8 *symfile_addr)
  204. {
  205. WASMJITEntryNode *node;
  206. node = bh_list_first_elem(&jit_debug_engine->jit_entry_list);
  207. while (node) {
  208. WASMJITEntryNode *next_node = bh_list_elem_next(node);
  209. if (node->entry->symfile_addr_ == symfile_addr) {
  210. DestroyJITCodeEntryInternal(node->entry);
  211. os_mutex_lock(&jit_debug_engine->jit_entry_lock);
  212. bh_list_remove(&jit_debug_engine->jit_entry_list, node);
  213. os_mutex_unlock(&jit_debug_engine->jit_entry_lock);
  214. wasm_runtime_free(node);
  215. }
  216. node = next_node;
  217. }
  218. }