jit_debug.c 6.7 KB

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