dwarf_extractor.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. * Copyright (C) 2021 Ant Group. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "lldb/API/SBBlock.h"
  6. #include "lldb/API/SBCompileUnit.h"
  7. #include "lldb/API/SBCommandReturnObject.h"
  8. #include "lldb/API/SBCommandInterpreter.h"
  9. #include "lldb/API/SBBreakpointLocation.h"
  10. #include "lldb/API/SBDebugger.h"
  11. #include "lldb/API//SBFunction.h"
  12. #include "lldb/API//SBModule.h"
  13. #include "lldb/API//SBProcess.h"
  14. #include "lldb/API//SBStream.h"
  15. #include "lldb/API//SBSymbol.h"
  16. #include "lldb/API//SBTarget.h"
  17. #include "lldb/API//SBThread.h"
  18. #include "lldb/API/SBDeclaration.h"
  19. #include "dwarf_extractor.h"
  20. #include "../aot_llvm.h"
  21. #include "bh_log.h"
  22. #include "../../aot/aot_runtime.h"
  23. #include "llvm/BinaryFormat/Dwarf.h"
  24. using namespace lldb;
  25. typedef struct dwarf_extractor {
  26. SBDebugger debugger;
  27. SBTarget target;
  28. SBModule module;
  29. } dwarf_extractor;
  30. #define TO_HANDLE(extractor) (dwarf_extractor_handle_t)(extractor)
  31. #define TO_EXTRACTOR(handle) (dwarf_extractor *)(handle)
  32. static const char *compiler_name = "WAMR AoT compiler";
  33. static bool is_debugger_initialized;
  34. dwarf_extractor_handle_t
  35. create_dwarf_extractor(AOTCompData *comp_data, char *file_name)
  36. {
  37. char *arch = NULL;
  38. char *platform = NULL;
  39. dwarf_extractor *extractor = NULL;
  40. //__attribute__((constructor)) may be better?
  41. if (!is_debugger_initialized) {
  42. SBError error = SBDebugger::InitializeWithErrorHandling();
  43. if (error.Fail()) {
  44. LOG_ERROR("Init Dwarf Debugger failed");
  45. return TO_HANDLE(NULL);
  46. }
  47. is_debugger_initialized = true;
  48. }
  49. SBError error;
  50. SBFileSpec exe_file_spec(file_name, true);
  51. if (!(extractor = new dwarf_extractor())) {
  52. LOG_ERROR("Create Dwarf Extractor error: failed to allocate memory");
  53. goto fail3;
  54. }
  55. extractor->debugger = SBDebugger::Create();
  56. if (!extractor->debugger.IsValid()) {
  57. LOG_ERROR("Create Dwarf Debugger failed");
  58. goto fail2;
  59. }
  60. extractor->target = extractor->debugger.CreateTarget(
  61. file_name, arch, platform, false, error);
  62. if (!error.Success()) {
  63. LOG_ERROR("Create Dwarf target failed:%s", error.GetCString());
  64. goto fail1;
  65. }
  66. if (!extractor->target.IsValid()) {
  67. LOG_ERROR("Create Dwarf target not valid");
  68. goto fail1;
  69. }
  70. extractor->module = extractor->target.FindModule(exe_file_spec);
  71. comp_data->extractor = TO_HANDLE(extractor);
  72. return TO_HANDLE(extractor);
  73. fail1:
  74. SBDebugger::Destroy(extractor->debugger);
  75. fail2:
  76. wasm_runtime_free(extractor);
  77. fail3:
  78. return TO_HANDLE(NULL);
  79. }
  80. void
  81. destroy_dwarf_extractor(dwarf_extractor_handle_t handle)
  82. {
  83. dwarf_extractor *extractor = TO_EXTRACTOR(handle);
  84. if (!extractor)
  85. return;
  86. extractor->debugger.DeleteTarget(extractor->target);
  87. SBDebugger::Destroy(extractor->debugger);
  88. delete extractor;
  89. SBDebugger::Terminate();
  90. is_debugger_initialized = false;
  91. }
  92. LLVMMetadataRef
  93. dwarf_gen_file_info(const AOTCompContext *comp_ctx)
  94. {
  95. dwarf_extractor *extractor;
  96. int units_number;
  97. LLVMMetadataRef file_info = NULL;
  98. const char *file_name;
  99. const char *dir_name;
  100. if (!(extractor = TO_EXTRACTOR(comp_ctx->comp_data->extractor)))
  101. return NULL;
  102. units_number = extractor->module.GetNumCompileUnits();
  103. if (units_number > 0) {
  104. SBCompileUnit compile_unit = extractor->module.GetCompileUnitAtIndex(0);
  105. auto filespec = compile_unit.GetFileSpec();
  106. file_name = filespec.GetFilename();
  107. dir_name = filespec.GetDirectory();
  108. if (file_name || dir_name) {
  109. file_info = LLVMDIBuilderCreateFile(
  110. comp_ctx->debug_builder, file_name,
  111. file_name ? strlen(file_name) : 0, dir_name,
  112. dir_name ? strlen(dir_name) : 0);
  113. }
  114. }
  115. return file_info;
  116. }
  117. #if 0
  118. void
  119. dwarf_gen_mock_vm_info(AOTCompContext *comp_ctx)
  120. {
  121. LLVMMetadataRef file_info = NULL;
  122. LLVMMetadataRef comp_unit = NULL;
  123. file_info = LLVMDIBuilderCreateFile(comp_ctx->debug_builder,
  124. "ant_runtime_mock.c", 18, ".", 1);
  125. comp_unit = LLVMDIBuilderCreateCompileUnit(
  126. comp_ctx->debug_builder, LLVMDWARFSourceLanguageC, file_info,
  127. "WAMR AoT compiler", 12, 0, NULL, 0, 1, NULL, 0, LLVMDWARFEmissionFull, 0, 0,
  128. 0, "/", 1, "", 0);
  129. LLVMTypeRef ParamTys[] = {
  130. LLVMVoidType(),
  131. };
  132. LLVMTypeRef FuncTy = LLVMFunctionType(LLVMVoidType(), ParamTys, 0, 0);
  133. LLVMValueRef Function =
  134. LLVMAddFunction(comp_ctx->module, "ant_runtime_mock", FuncTy);
  135. LLVMMetadataRef ParamTypes[0];
  136. LLVMMetadataRef FunctionTy = LLVMDIBuilderCreateSubroutineType(
  137. comp_ctx->debug_builder, file_info, ParamTypes, 0, LLVMDIFlagZero);
  138. /* 0x0015 is subroutine_type */
  139. LLVMMetadataRef ReplaceableFunctionMetadata =
  140. LLVMDIBuilderCreateReplaceableCompositeType(
  141. comp_ctx->debug_builder, 0x15, "ant_runtime_mock", 16, file_info,
  142. file_info, 2, 0, 0, 0, LLVMDIFlagFwdDecl, "", 0);
  143. LLVMMetadataRef FunctionMetadata = LLVMDIBuilderCreateFunction(
  144. comp_ctx->debug_builder, file_info, "ant_runtime_mock", 16,
  145. "ant_runtime_mock", 16, file_info, 2, FunctionTy, true, true, 2, LLVMDIFlagZero,
  146. false);
  147. LLVMMetadataReplaceAllUsesWith(ReplaceableFunctionMetadata,
  148. FunctionMetadata);
  149. LLVMSetSubprogram(Function, FunctionMetadata);
  150. comp_ctx->vm_debug_comp_unit = comp_unit;
  151. comp_ctx->vm_debug_file = file_info;
  152. comp_ctx->vm_debug_func = FunctionMetadata;
  153. }
  154. #endif
  155. LLVMMetadataRef
  156. dwarf_gen_comp_unit_info(const AOTCompContext *comp_ctx)
  157. {
  158. dwarf_extractor *extractor;
  159. int units_number;
  160. LLVMMetadataRef comp_unit = NULL;
  161. if (!(extractor = TO_EXTRACTOR(comp_ctx->comp_data->extractor)))
  162. return NULL;
  163. units_number = extractor->module.GetNumCompileUnits();
  164. if (units_number > 0) {
  165. SBCompileUnit compile_unit = extractor->module.GetCompileUnitAtIndex(0);
  166. auto lang_type = compile_unit.GetLanguage();
  167. comp_unit = LLVMDIBuilderCreateCompileUnit(
  168. comp_ctx->debug_builder, LLDB_TO_LLVM_LANG_TYPE(lang_type),
  169. comp_ctx->debug_file, compiler_name, strlen(compiler_name), 0, NULL,
  170. 0, 1, NULL, 0, LLVMDWARFEmissionFull, 0, 0, 0, "/", 1, "", 0);
  171. }
  172. return comp_unit;
  173. }
  174. static LLVMDWARFTypeEncoding
  175. lldb_get_basic_type_encoding(BasicType basic_type)
  176. {
  177. LLVMDWARFTypeEncoding encoding = 0;
  178. switch (basic_type) {
  179. case eBasicTypeUnsignedChar:
  180. encoding = llvm::dwarf::DW_ATE_unsigned_char;
  181. break;
  182. case eBasicTypeSignedChar:
  183. encoding = llvm::dwarf::DW_ATE_signed_char;
  184. break;
  185. case eBasicTypeUnsignedInt:
  186. case eBasicTypeUnsignedLong:
  187. case eBasicTypeUnsignedLongLong:
  188. case eBasicTypeUnsignedWChar:
  189. case eBasicTypeUnsignedInt128:
  190. case eBasicTypeUnsignedShort:
  191. encoding = llvm::dwarf::DW_ATE_unsigned;
  192. break;
  193. case eBasicTypeInt:
  194. case eBasicTypeLong:
  195. case eBasicTypeLongLong:
  196. case eBasicTypeWChar:
  197. case eBasicTypeInt128:
  198. case eBasicTypeShort:
  199. encoding = llvm::dwarf::DW_ATE_signed;
  200. break;
  201. case eBasicTypeBool:
  202. encoding = llvm::dwarf::DW_ATE_boolean;
  203. break;
  204. case eBasicTypeHalf:
  205. case eBasicTypeFloat:
  206. case eBasicTypeDouble:
  207. case eBasicTypeLongDouble:
  208. encoding = llvm::dwarf::DW_ATE_float;
  209. break;
  210. default:
  211. break;
  212. }
  213. return encoding;
  214. }
  215. static LLVMMetadataRef
  216. lldb_type_to_type_dbi(const AOTCompContext *comp_ctx, SBType &type)
  217. {
  218. LLVMMetadataRef type_info = NULL;
  219. BasicType basic_type = type.GetBasicType();
  220. uint64_t bit_size = type.GetByteSize() * 8;
  221. LLVMDIBuilderRef DIB = comp_ctx->debug_builder;
  222. LLVMDWARFTypeEncoding encoding;
  223. if (basic_type != eBasicTypeInvalid) {
  224. encoding = lldb_get_basic_type_encoding(basic_type);
  225. type_info = LLVMDIBuilderCreateBasicType(
  226. DIB, type.GetName(), strlen(type.GetName()), bit_size, encoding,
  227. LLVMDIFlagZero);
  228. }
  229. else if (type.IsPointerType()) {
  230. SBType pointee_type = type.GetPointeeType();
  231. type_info = LLVMDIBuilderCreatePointerType(
  232. DIB, lldb_type_to_type_dbi(comp_ctx, pointee_type), bit_size, 0, 0,
  233. "", 0);
  234. }
  235. return type_info;
  236. }
  237. static LLVMMetadataRef
  238. lldb_function_to_function_dbi(const AOTCompContext *comp_ctx,
  239. SBSymbolContext &sc,
  240. const AOTFuncContext *func_ctx)
  241. {
  242. SBFunction function(sc.GetFunction());
  243. const char *function_name = function.GetName();
  244. const char *link_name = function.GetMangledName();
  245. SBTypeList function_args = function.GetType().GetFunctionArgumentTypes();
  246. SBType return_type = function.GetType().GetFunctionReturnType();
  247. const size_t num_function_args = function_args.GetSize();
  248. dwarf_extractor *extractor;
  249. /*
  250. * Process only known languages.
  251. * We have a few assumptions which might not be true for non-C functions.
  252. *
  253. * At least it's known broken for C++ and Rust:
  254. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/3187
  255. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/3163
  256. */
  257. LanguageType language_type = function.GetLanguage();
  258. bool cplusplus = false;
  259. switch (language_type) {
  260. case eLanguageTypeC89:
  261. case eLanguageTypeC:
  262. case eLanguageTypeC99:
  263. case eLanguageTypeC11:
  264. #if LLVM_VERSION_MAJOR >= 17
  265. case eLanguageTypeC17:
  266. #endif
  267. break;
  268. case eLanguageTypeC_plus_plus:
  269. case eLanguageTypeC_plus_plus_03:
  270. case eLanguageTypeC_plus_plus_11:
  271. case eLanguageTypeC_plus_plus_14:
  272. #if LLVM_VERSION_MAJOR >= 17
  273. case eLanguageTypeC_plus_plus_17:
  274. case eLanguageTypeC_plus_plus_20:
  275. #endif
  276. cplusplus = true;
  277. break;
  278. default:
  279. LOG_WARNING("func %s has unsupported language_type 0x%x",
  280. function_name, (int)language_type);
  281. return NULL;
  282. }
  283. if (!(extractor = TO_EXTRACTOR(comp_ctx->comp_data->extractor)))
  284. return NULL;
  285. LLVMDIBuilderRef DIB = comp_ctx->debug_builder;
  286. LLVMMetadataRef File = comp_ctx->debug_file; /* a fallback */
  287. LLVMMetadataRef ParamTypes[num_function_args + 1];
  288. size_t num_param_types = 0;
  289. if (!cplusplus) {
  290. num_param_types = num_function_args + 1;
  291. ParamTypes[0] = lldb_type_to_type_dbi(comp_ctx, return_type);
  292. for (uint32_t function_arg_idx = 0;
  293. function_arg_idx < num_function_args; ++function_arg_idx) {
  294. SBType function_arg_type =
  295. function_args.GetTypeAtIndex(function_arg_idx);
  296. if (function_arg_type.IsValid()) {
  297. ParamTypes[function_arg_idx + 1] =
  298. lldb_type_to_type_dbi(comp_ctx, function_arg_type);
  299. if (ParamTypes[function_arg_idx + 1] == NULL) {
  300. LOG_WARNING(
  301. "func %s arg %" PRIu32
  302. " has a type not implemented by lldb_type_to_type_dbi",
  303. function_name, function_arg_idx);
  304. }
  305. }
  306. else {
  307. LOG_WARNING("func %s arg %" PRIu32 ": GetTypeAtIndex failed",
  308. function_name, function_arg_idx);
  309. ParamTypes[function_arg_idx + 1] = NULL;
  310. }
  311. }
  312. }
  313. auto compile_unit = sc.GetCompileUnit();
  314. auto file_spec = compile_unit.GetFileSpec();
  315. const char *file_name = file_spec.GetFilename();
  316. const char *dir_name = file_spec.GetDirectory();
  317. LLVMMetadataRef file_info = NULL;
  318. if (file_name || dir_name) {
  319. file_info =
  320. LLVMDIBuilderCreateFile(comp_ctx->debug_builder, file_name,
  321. file_name ? strlen(file_name) : 0, dir_name,
  322. dir_name ? strlen(dir_name) : 0);
  323. }
  324. if (file_info) {
  325. File = file_info;
  326. }
  327. LLVMMetadataRef FunctionTy = LLVMDIBuilderCreateSubroutineType(
  328. DIB, File, ParamTypes, num_param_types, LLVMDIFlagZero);
  329. auto line_entry = sc.GetLineEntry();
  330. LLVMMetadataRef ReplaceableFunctionMetadata =
  331. LLVMDIBuilderCreateReplaceableCompositeType(
  332. DIB, 0x15, function_name, strlen(function_name), File, File,
  333. line_entry.GetLine(), 0, 0, 0, LLVMDIFlagFwdDecl, "", 0);
  334. LLVMMetadataRef FunctionMetadata = LLVMDIBuilderCreateFunction(
  335. DIB, File, function_name, strlen(function_name), link_name,
  336. link_name != NULL ? strlen(link_name) : 0, File, line_entry.GetLine(),
  337. FunctionTy, true, true, line_entry.GetLine(), LLVMDIFlagZero, false);
  338. LLVMMetadataReplaceAllUsesWith(ReplaceableFunctionMetadata,
  339. FunctionMetadata);
  340. LLVMSetSubprogram(func_ctx->func, FunctionMetadata);
  341. LLVMMetadataRef ParamExpression =
  342. LLVMDIBuilderCreateExpression(DIB, NULL, 0);
  343. LLVMMetadataRef ParamLocation = LLVMDIBuilderCreateDebugLocation(
  344. comp_ctx->context, line_entry.GetLine(), 0, FunctionMetadata, NULL);
  345. // TODO:change to void * or WasmExenv * ?
  346. LLVMMetadataRef voidtype =
  347. LLVMDIBuilderCreateBasicType(DIB, "void", 4, 0, 0, LLVMDIFlagZero);
  348. LLVMMetadataRef voidpointer =
  349. LLVMDIBuilderCreatePointerType(DIB, voidtype, 64, 0, 0, "void *", 6);
  350. LLVMMetadataRef ParamVar = LLVMDIBuilderCreateParameterVariable(
  351. DIB, FunctionMetadata, "exenv", 5, 1,
  352. File, // starts form 1, and 1 is exenv,
  353. line_entry.GetLine(), voidpointer, true, LLVMDIFlagZero);
  354. LLVMValueRef Param = LLVMGetParam(func_ctx->func, 0);
  355. LLVMBasicBlockRef block_curr = LLVMGetEntryBasicBlock(func_ctx->func);
  356. LLVMDIBuilderInsertDbgValueAtEnd(DIB, Param, ParamVar, ParamExpression,
  357. ParamLocation, block_curr);
  358. if (num_function_args != func_ctx->aot_func->func_type->param_count) {
  359. // for C, this happens when the compiler optimized out some of
  360. // function parameters.
  361. //
  362. // for C++, this mismatch is normal because of the "this" pointer.
  363. if (!cplusplus) {
  364. LOG_WARNING("function args number mismatch! num_function_args: %d, "
  365. "wasm func params: %d, func: %s",
  366. num_function_args,
  367. func_ctx->aot_func->func_type->param_count,
  368. function_name);
  369. }
  370. }
  371. else if (!cplusplus) {
  372. auto variable_list = function.GetBlock().GetVariables(
  373. extractor->target, true, false, false);
  374. if (num_function_args != variable_list.GetSize()) {
  375. LOG_ERROR("function args number mismatch!:value number=%d, "
  376. "function args=%d",
  377. variable_list.GetSize(), num_function_args);
  378. }
  379. for (uint32_t function_arg_idx = 0;
  380. function_arg_idx < variable_list.GetSize(); ++function_arg_idx) {
  381. SBValue variable(variable_list.GetValueAtIndex(function_arg_idx));
  382. if (variable.IsValid()
  383. && ParamTypes[function_arg_idx + 1] != NULL) {
  384. SBDeclaration dec(variable.GetDeclaration());
  385. auto valtype = variable.GetType();
  386. LLVMMetadataRef ParamLocation =
  387. LLVMDIBuilderCreateDebugLocation(
  388. comp_ctx->context, dec.GetLine(), dec.GetColumn(),
  389. FunctionMetadata, NULL);
  390. const char *varname = variable.GetName();
  391. LLVMMetadataRef ParamVar = LLVMDIBuilderCreateParameterVariable(
  392. DIB, FunctionMetadata, varname,
  393. varname ? strlen(varname) : 0, function_arg_idx + 1 + 1,
  394. File, // starts form 1, and 1 is exenv,
  395. dec.GetLine(), ParamTypes[function_arg_idx + 1], true,
  396. LLVMDIFlagZero);
  397. LLVMValueRef Param =
  398. LLVMGetParam(func_ctx->func, function_arg_idx + 1);
  399. LLVMDIBuilderInsertDbgValueAtEnd(DIB, Param, ParamVar,
  400. ParamExpression, ParamLocation,
  401. block_curr);
  402. }
  403. }
  404. }
  405. return FunctionMetadata;
  406. }
  407. LLVMMetadataRef
  408. dwarf_gen_func_info(const AOTCompContext *comp_ctx,
  409. const AOTFuncContext *func_ctx)
  410. {
  411. LLVMMetadataRef func_info = NULL;
  412. dwarf_extractor *extractor;
  413. uint64_t vm_offset;
  414. AOTFunc *func = func_ctx->aot_func;
  415. if (!(extractor = TO_EXTRACTOR(comp_ctx->comp_data->extractor)))
  416. return NULL;
  417. // A code address in DWARF for WebAssembly is the offset of an
  418. // instruction relative within the Code section of the WebAssembly file.
  419. // For this reason Section::GetFileAddress() must return zero for the
  420. // Code section. (refer to ObjectFileWasm.cpp)
  421. vm_offset = func->code - comp_ctx->comp_data->wasm_module->buf_code;
  422. auto sbaddr = extractor->target.ResolveFileAddress(vm_offset);
  423. SBSymbolContext sc(sbaddr.GetSymbolContext(eSymbolContextFunction
  424. | eSymbolContextLineEntry));
  425. if (sc.IsValid()) {
  426. SBFunction function(sc.GetFunction());
  427. if (function.IsValid()) {
  428. func_info = lldb_function_to_function_dbi(comp_ctx, sc, func_ctx);
  429. }
  430. }
  431. return func_info;
  432. }
  433. void
  434. dwarf_get_func_name(const AOTCompContext *comp_ctx,
  435. const AOTFuncContext *func_ctx, char *name, int len)
  436. {
  437. LLVMMetadataRef func_info = NULL;
  438. dwarf_extractor *extractor;
  439. uint64_t vm_offset;
  440. AOTFunc *func = func_ctx->aot_func;
  441. name[0] = '\0';
  442. if (!(extractor = TO_EXTRACTOR(comp_ctx->comp_data->extractor)))
  443. return;
  444. // A code address in DWARF for WebAssembly is the offset of an
  445. // instruction relative within the Code section of the WebAssembly file.
  446. // For this reason Section::GetFileAddress() must return zero for the
  447. // Code section. (refer to ObjectFileWasm.cpp)
  448. vm_offset = func->code - comp_ctx->comp_data->wasm_module->buf_code;
  449. auto sbaddr = extractor->target.ResolveFileAddress(vm_offset);
  450. SBSymbolContext sc(sbaddr.GetSymbolContext(eSymbolContextFunction
  451. | eSymbolContextLineEntry));
  452. if (sc.IsValid()) {
  453. SBFunction function(sc.GetFunction());
  454. if (function.IsValid()) {
  455. bh_strcpy_s(name, len, function.GetName());
  456. }
  457. }
  458. }
  459. LLVMMetadataRef
  460. dwarf_gen_location(const AOTCompContext *comp_ctx,
  461. const AOTFuncContext *func_ctx, uint64_t vm_offset)
  462. {
  463. LLVMMetadataRef location_info = NULL;
  464. dwarf_extractor *extractor;
  465. AOTFunc *func = func_ctx->aot_func;
  466. if (func_ctx->debug_func == NULL)
  467. return NULL;
  468. if (!(extractor = TO_EXTRACTOR(comp_ctx->comp_data->extractor)))
  469. return NULL;
  470. auto sbaddr = extractor->target.ResolveFileAddress(vm_offset);
  471. SBSymbolContext sc(sbaddr.GetSymbolContext(eSymbolContextFunction
  472. | eSymbolContextLineEntry));
  473. if (sc.IsValid()) {
  474. // TODO:need to check if the vm_offset is belong to
  475. SBFunction function(sc.GetFunction());
  476. if (function.IsValid()) {
  477. uint64_t start = func_ctx->aot_func->code
  478. - comp_ctx->comp_data->wasm_module->buf_code;
  479. uint64_t end = func_ctx->aot_func->code
  480. - comp_ctx->comp_data->wasm_module->buf_code
  481. + func_ctx->aot_func->code_size;
  482. if (function.GetStartAddress().GetOffset() <= start
  483. && end <= function.GetEndAddress().GetOffset()) {
  484. auto line_entry = sc.GetLineEntry();
  485. location_info = LLVMDIBuilderCreateDebugLocation(
  486. comp_ctx->context, line_entry.GetLine(),
  487. line_entry.GetColumn(), func_ctx->debug_func, NULL);
  488. // LOG_VERBOSE("Gen the location l:%d, c:%d at %lx",
  489. // line_entry.GetLine(), line_entry.GetColumn(), vm_offset);
  490. }
  491. else
  492. LOG_WARNING("the offset and function is not matched");
  493. }
  494. }
  495. return location_info;
  496. }
  497. LLVMMetadataRef
  498. dwarf_gen_func_ret_location(const AOTCompContext *comp_ctx,
  499. const AOTFuncContext *func_ctx)
  500. {
  501. LLVMMetadataRef func_info = NULL;
  502. dwarf_extractor *extractor;
  503. uint64_t vm_offset;
  504. AOTFunc *func = func_ctx->aot_func;
  505. LLVMMetadataRef location_info = NULL;
  506. if (!(extractor = TO_EXTRACTOR(comp_ctx->comp_data->extractor)))
  507. return NULL;
  508. // A code address in DWARF for WebAssembly is the offset of an
  509. // instruction relative within the Code section of the WebAssembly file.
  510. // For this reason Section::GetFileAddress() must return zero for the
  511. // Code section. (refer to ObjectFileWasm.cpp)
  512. vm_offset = (func->code + func->code_size - 1)
  513. - comp_ctx->comp_data->wasm_module->buf_code;
  514. location_info = dwarf_gen_location(comp_ctx, func_ctx, vm_offset);
  515. return location_info;
  516. }