jit_debug.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <fcntl.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <string.h>
  28. #include <errno.h>
  29. #include <stdbool.h>
  30. /* This must be kept in sync with gdb/gdb/jit.h */
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /* clang-format off */
  35. typedef enum JITAction {
  36. JIT_NOACTION = 0,
  37. JIT_REGISTER_FN,
  38. JIT_UNREGISTER_FN
  39. } JITAction;
  40. /* clang-format on */
  41. typedef struct JITCodeEntry {
  42. struct JITCodeEntry *next_;
  43. struct JITCodeEntry *prev_;
  44. const uint8 *symfile_addr_;
  45. uint64 symfile_size_;
  46. } JITCodeEntry;
  47. typedef struct JITDescriptor {
  48. uint32 version_;
  49. uint32 action_flag_;
  50. JITCodeEntry *relevant_entry_;
  51. JITCodeEntry *first_entry_;
  52. } JITDescriptor;
  53. /* LLVM has already define this */
  54. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  55. /**
  56. * GDB will place breakpoint into this function.
  57. * To prevent GCC from inlining or removing it we place noinline attribute
  58. * and inline assembler statement inside.
  59. */
  60. void __attribute__((noinline)) __jit_debug_register_code();
  61. void __attribute__((noinline)) __jit_debug_register_code()
  62. {
  63. int x;
  64. *(char *)&x = '\0';
  65. }
  66. /**
  67. * GDB will inspect contents of this descriptor.
  68. * Static initialization is necessary to prevent GDB from seeing
  69. * uninitialized descriptor.
  70. */
  71. JITDescriptor __jit_debug_descriptor = { 1, JIT_NOACTION, NULL, NULL };
  72. #else
  73. extern void
  74. __jit_debug_register_code();
  75. extern JITDescriptor __jit_debug_descriptor;
  76. #endif
  77. /**
  78. * Call __jit_debug_register_code indirectly via global variable.
  79. * This gives the debugger an easy way to inject custom code to
  80. * handle the events.
  81. */
  82. void (*__jit_debug_register_code_ptr)() = __jit_debug_register_code;
  83. #ifdef __cplusplus
  84. }
  85. #endif
  86. typedef struct WASMJITDebugEngine {
  87. korp_mutex jit_entry_lock;
  88. bh_list jit_entry_list;
  89. } WASMJITDebugEngine;
  90. typedef struct WASMJITEntryNode {
  91. struct WASMJITEntryNode *next;
  92. JITCodeEntry *entry;
  93. } WASMJITEntryNode;
  94. static WASMJITDebugEngine *jit_debug_engine;
  95. static JITCodeEntry *
  96. CreateJITCodeEntryInternal(const uint8 *symfile_addr, uint64 symfile_size)
  97. {
  98. JITCodeEntry *entry;
  99. os_mutex_lock(&jit_debug_engine->jit_entry_lock);
  100. if (!(entry = wasm_runtime_malloc(sizeof(JITCodeEntry)))) {
  101. LOG_ERROR("WASM JIT Debug Engine error: failed to allocate memory");
  102. os_mutex_unlock(&jit_debug_engine->jit_entry_lock);
  103. return NULL;
  104. }
  105. entry->symfile_addr_ = symfile_addr;
  106. entry->symfile_size_ = symfile_size;
  107. entry->prev_ = NULL;
  108. entry->next_ = __jit_debug_descriptor.first_entry_;
  109. if (entry->next_ != NULL) {
  110. entry->next_->prev_ = entry;
  111. }
  112. __jit_debug_descriptor.first_entry_ = entry;
  113. __jit_debug_descriptor.relevant_entry_ = entry;
  114. __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN;
  115. (*__jit_debug_register_code_ptr)();
  116. os_mutex_unlock(&jit_debug_engine->jit_entry_lock);
  117. return entry;
  118. }
  119. static void
  120. DestroyJITCodeEntryInternal(JITCodeEntry *entry)
  121. {
  122. os_mutex_lock(&jit_debug_engine->jit_entry_lock);
  123. if (entry->prev_ != NULL) {
  124. entry->prev_->next_ = entry->next_;
  125. }
  126. else {
  127. __jit_debug_descriptor.first_entry_ = entry->next_;
  128. }
  129. if (entry->next_ != NULL) {
  130. entry->next_->prev_ = entry->prev_;
  131. }
  132. __jit_debug_descriptor.relevant_entry_ = entry;
  133. __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN;
  134. (*__jit_debug_register_code_ptr)();
  135. wasm_runtime_free(entry);
  136. os_mutex_unlock(&jit_debug_engine->jit_entry_lock);
  137. }
  138. bool
  139. jit_debug_engine_init()
  140. {
  141. if (jit_debug_engine) {
  142. return true;
  143. }
  144. if (!(jit_debug_engine = wasm_runtime_malloc(sizeof(WASMJITDebugEngine)))) {
  145. LOG_ERROR("WASM JIT Debug Engine error: failed to allocate memory");
  146. return false;
  147. }
  148. memset(jit_debug_engine, 0, sizeof(WASMJITDebugEngine));
  149. if (os_mutex_init(&jit_debug_engine->jit_entry_lock) != 0) {
  150. wasm_runtime_free(jit_debug_engine);
  151. jit_debug_engine = NULL;
  152. return false;
  153. }
  154. bh_list_init(&jit_debug_engine->jit_entry_list);
  155. return true;
  156. }
  157. void
  158. jit_debug_engine_destroy()
  159. {
  160. if (jit_debug_engine) {
  161. WASMJITEntryNode *node, *node_next;
  162. /* Destroy all nodes */
  163. node = bh_list_first_elem(&jit_debug_engine->jit_entry_list);
  164. while (node) {
  165. node_next = bh_list_elem_next(node);
  166. DestroyJITCodeEntryInternal(node->entry);
  167. bh_list_remove(&jit_debug_engine->jit_entry_list, node);
  168. wasm_runtime_free(node);
  169. node = node_next;
  170. }
  171. /* Destroy JIT Debug Engine */
  172. os_mutex_destroy(&jit_debug_engine->jit_entry_lock);
  173. wasm_runtime_free(jit_debug_engine);
  174. jit_debug_engine = NULL;
  175. }
  176. }
  177. bool
  178. jit_code_entry_create(const uint8 *symfile_addr, uint64 symfile_size)
  179. {
  180. JITCodeEntry *entry;
  181. WASMJITEntryNode *node;
  182. if (!(node = wasm_runtime_malloc(sizeof(WASMJITEntryNode)))) {
  183. LOG_ERROR("WASM JIT Debug Engine error: failed to allocate memory");
  184. return false;
  185. }
  186. entry = CreateJITCodeEntryInternal(symfile_addr, symfile_size);
  187. if (!entry) {
  188. wasm_runtime_free(node);
  189. return false;
  190. }
  191. node->entry = entry;
  192. os_mutex_lock(&jit_debug_engine->jit_entry_lock);
  193. bh_list_insert(&jit_debug_engine->jit_entry_list, node);
  194. os_mutex_unlock(&jit_debug_engine->jit_entry_lock);
  195. return true;
  196. }
  197. void
  198. jit_code_entry_destroy(const uint8 *symfile_addr)
  199. {
  200. WASMJITEntryNode *node;
  201. node = bh_list_first_elem(&jit_debug_engine->jit_entry_list);
  202. while (node) {
  203. WASMJITEntryNode *next_node = bh_list_elem_next(node);
  204. if (node->entry->symfile_addr_ == symfile_addr) {
  205. DestroyJITCodeEntryInternal(node->entry);
  206. os_mutex_lock(&jit_debug_engine->jit_entry_lock);
  207. bh_list_remove(&jit_debug_engine->jit_entry_list, node);
  208. os_mutex_unlock(&jit_debug_engine->jit_entry_lock);
  209. wasm_runtime_free(node);
  210. }
  211. node = next_node;
  212. }
  213. }