dwarf_extractor.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. case eLanguageTypeC17:
  265. break;
  266. case eLanguageTypeC_plus_plus:
  267. case eLanguageTypeC_plus_plus_03:
  268. case eLanguageTypeC_plus_plus_11:
  269. case eLanguageTypeC_plus_plus_14:
  270. case eLanguageTypeC_plus_plus_17:
  271. case eLanguageTypeC_plus_plus_20:
  272. cplusplus = true;
  273. break;
  274. default:
  275. LOG_WARNING("func %s has unsupported language_type 0x%x",
  276. function_name, (int)language_type);
  277. return NULL;
  278. }
  279. if (!(extractor = TO_EXTRACTOR(comp_ctx->comp_data->extractor)))
  280. return NULL;
  281. LLVMDIBuilderRef DIB = comp_ctx->debug_builder;
  282. LLVMMetadataRef File = comp_ctx->debug_file; /* a fallback */
  283. LLVMMetadataRef ParamTypes[num_function_args + 1];
  284. size_t num_param_types = 0;
  285. if (!cplusplus) {
  286. num_param_types = num_function_args + 1;
  287. ParamTypes[0] = lldb_type_to_type_dbi(comp_ctx, return_type);
  288. for (uint32_t function_arg_idx = 0;
  289. function_arg_idx < num_function_args; ++function_arg_idx) {
  290. SBType function_arg_type =
  291. function_args.GetTypeAtIndex(function_arg_idx);
  292. if (function_arg_type.IsValid()) {
  293. ParamTypes[function_arg_idx + 1] =
  294. lldb_type_to_type_dbi(comp_ctx, function_arg_type);
  295. if (ParamTypes[function_arg_idx + 1] == NULL) {
  296. LOG_WARNING(
  297. "func %s arg %" PRIu32
  298. " has a type not implemented by lldb_type_to_type_dbi",
  299. function_name, function_arg_idx);
  300. }
  301. }
  302. else {
  303. LOG_WARNING("func %s arg %" PRIu32 ": GetTypeAtIndex failed",
  304. function_name, function_arg_idx);
  305. ParamTypes[function_arg_idx + 1] = NULL;
  306. }
  307. }
  308. }
  309. auto compile_unit = sc.GetCompileUnit();
  310. auto file_spec = compile_unit.GetFileSpec();
  311. const char *file_name = file_spec.GetFilename();
  312. const char *dir_name = file_spec.GetDirectory();
  313. LLVMMetadataRef file_info = NULL;
  314. if (file_name || dir_name) {
  315. file_info =
  316. LLVMDIBuilderCreateFile(comp_ctx->debug_builder, file_name,
  317. file_name ? strlen(file_name) : 0, dir_name,
  318. dir_name ? strlen(dir_name) : 0);
  319. }
  320. if (file_info) {
  321. File = file_info;
  322. }
  323. LLVMMetadataRef FunctionTy = LLVMDIBuilderCreateSubroutineType(
  324. DIB, File, ParamTypes, num_param_types, LLVMDIFlagZero);
  325. auto line_entry = sc.GetLineEntry();
  326. LLVMMetadataRef ReplaceableFunctionMetadata =
  327. LLVMDIBuilderCreateReplaceableCompositeType(
  328. DIB, 0x15, function_name, strlen(function_name), File, File,
  329. line_entry.GetLine(), 0, 0, 0, LLVMDIFlagFwdDecl, "", 0);
  330. LLVMMetadataRef FunctionMetadata = LLVMDIBuilderCreateFunction(
  331. DIB, File, function_name, strlen(function_name), link_name,
  332. link_name != NULL ? strlen(link_name) : 0, File, line_entry.GetLine(),
  333. FunctionTy, true, true, line_entry.GetLine(), LLVMDIFlagZero, false);
  334. LLVMMetadataReplaceAllUsesWith(ReplaceableFunctionMetadata,
  335. FunctionMetadata);
  336. LLVMSetSubprogram(func_ctx->func, FunctionMetadata);
  337. LLVMMetadataRef ParamExpression =
  338. LLVMDIBuilderCreateExpression(DIB, NULL, 0);
  339. LLVMMetadataRef ParamLocation = LLVMDIBuilderCreateDebugLocation(
  340. comp_ctx->context, line_entry.GetLine(), 0, FunctionMetadata, NULL);
  341. // TODO:change to void * or WasmExenv * ?
  342. LLVMMetadataRef voidtype =
  343. LLVMDIBuilderCreateBasicType(DIB, "void", 4, 0, 0, LLVMDIFlagZero);
  344. LLVMMetadataRef voidpointer =
  345. LLVMDIBuilderCreatePointerType(DIB, voidtype, 64, 0, 0, "void *", 6);
  346. LLVMMetadataRef ParamVar = LLVMDIBuilderCreateParameterVariable(
  347. DIB, FunctionMetadata, "exenv", 5, 1,
  348. File, // starts form 1, and 1 is exenv,
  349. line_entry.GetLine(), voidpointer, true, LLVMDIFlagZero);
  350. LLVMValueRef Param = LLVMGetParam(func_ctx->func, 0);
  351. LLVMBasicBlockRef block_curr = LLVMGetEntryBasicBlock(func_ctx->func);
  352. LLVMDIBuilderInsertDbgValueAtEnd(DIB, Param, ParamVar, ParamExpression,
  353. ParamLocation, block_curr);
  354. if (num_function_args != func_ctx->aot_func->func_type->param_count) {
  355. // for C, this happens when the compiler optimized out some of
  356. // function parameters.
  357. //
  358. // for C++, this mismatch is normal because of the "this" pointer.
  359. if (!cplusplus) {
  360. LOG_WARNING("function args number mismatch! num_function_args: %d, "
  361. "wasm func params: %d, func: %s",
  362. num_function_args,
  363. func_ctx->aot_func->func_type->param_count,
  364. function_name);
  365. }
  366. }
  367. else if (!cplusplus) {
  368. auto variable_list = function.GetBlock().GetVariables(
  369. extractor->target, true, false, false);
  370. if (num_function_args != variable_list.GetSize()) {
  371. LOG_ERROR("function args number mismatch!:value number=%d, "
  372. "function args=%d",
  373. variable_list.GetSize(), num_function_args);
  374. }
  375. for (uint32_t function_arg_idx = 0;
  376. function_arg_idx < variable_list.GetSize(); ++function_arg_idx) {
  377. SBValue variable(variable_list.GetValueAtIndex(function_arg_idx));
  378. if (variable.IsValid()
  379. && ParamTypes[function_arg_idx + 1] != NULL) {
  380. SBDeclaration dec(variable.GetDeclaration());
  381. auto valtype = variable.GetType();
  382. LLVMMetadataRef ParamLocation =
  383. LLVMDIBuilderCreateDebugLocation(
  384. comp_ctx->context, dec.GetLine(), dec.GetColumn(),
  385. FunctionMetadata, NULL);
  386. const char *varname = variable.GetName();
  387. LLVMMetadataRef ParamVar = LLVMDIBuilderCreateParameterVariable(
  388. DIB, FunctionMetadata, varname,
  389. varname ? strlen(varname) : 0, function_arg_idx + 1 + 1,
  390. File, // starts form 1, and 1 is exenv,
  391. dec.GetLine(), ParamTypes[function_arg_idx + 1], true,
  392. LLVMDIFlagZero);
  393. LLVMValueRef Param =
  394. LLVMGetParam(func_ctx->func, function_arg_idx + 1);
  395. LLVMDIBuilderInsertDbgValueAtEnd(DIB, Param, ParamVar,
  396. ParamExpression, ParamLocation,
  397. block_curr);
  398. }
  399. }
  400. }
  401. return FunctionMetadata;
  402. }
  403. LLVMMetadataRef
  404. dwarf_gen_func_info(const AOTCompContext *comp_ctx,
  405. const AOTFuncContext *func_ctx)
  406. {
  407. LLVMMetadataRef func_info = NULL;
  408. dwarf_extractor *extractor;
  409. uint64_t vm_offset;
  410. AOTFunc *func = func_ctx->aot_func;
  411. if (!(extractor = TO_EXTRACTOR(comp_ctx->comp_data->extractor)))
  412. return NULL;
  413. // A code address in DWARF for WebAssembly is the offset of an
  414. // instruction relative within the Code section of the WebAssembly file.
  415. // For this reason Section::GetFileAddress() must return zero for the
  416. // Code section. (refer to ObjectFileWasm.cpp)
  417. vm_offset = func->code - comp_ctx->comp_data->wasm_module->buf_code;
  418. auto sbaddr = extractor->target.ResolveFileAddress(vm_offset);
  419. SBSymbolContext sc(sbaddr.GetSymbolContext(eSymbolContextFunction
  420. | eSymbolContextLineEntry));
  421. if (sc.IsValid()) {
  422. SBFunction function(sc.GetFunction());
  423. if (function.IsValid()) {
  424. func_info = lldb_function_to_function_dbi(comp_ctx, sc, func_ctx);
  425. }
  426. }
  427. return func_info;
  428. }
  429. void
  430. dwarf_get_func_name(const AOTCompContext *comp_ctx,
  431. const AOTFuncContext *func_ctx, char *name, int len)
  432. {
  433. LLVMMetadataRef func_info = NULL;
  434. dwarf_extractor *extractor;
  435. uint64_t vm_offset;
  436. AOTFunc *func = func_ctx->aot_func;
  437. name[0] = '\0';
  438. if (!(extractor = TO_EXTRACTOR(comp_ctx->comp_data->extractor)))
  439. return;
  440. // A code address in DWARF for WebAssembly is the offset of an
  441. // instruction relative within the Code section of the WebAssembly file.
  442. // For this reason Section::GetFileAddress() must return zero for the
  443. // Code section. (refer to ObjectFileWasm.cpp)
  444. vm_offset = func->code - comp_ctx->comp_data->wasm_module->buf_code;
  445. auto sbaddr = extractor->target.ResolveFileAddress(vm_offset);
  446. SBSymbolContext sc(sbaddr.GetSymbolContext(eSymbolContextFunction
  447. | eSymbolContextLineEntry));
  448. if (sc.IsValid()) {
  449. SBFunction function(sc.GetFunction());
  450. if (function.IsValid()) {
  451. bh_strcpy_s(name, len, function.GetName());
  452. }
  453. }
  454. }
  455. LLVMMetadataRef
  456. dwarf_gen_location(const AOTCompContext *comp_ctx,
  457. const AOTFuncContext *func_ctx, uint64_t vm_offset)
  458. {
  459. LLVMMetadataRef location_info = NULL;
  460. dwarf_extractor *extractor;
  461. AOTFunc *func = func_ctx->aot_func;
  462. if (func_ctx->debug_func == NULL)
  463. return NULL;
  464. if (!(extractor = TO_EXTRACTOR(comp_ctx->comp_data->extractor)))
  465. return NULL;
  466. auto sbaddr = extractor->target.ResolveFileAddress(vm_offset);
  467. SBSymbolContext sc(sbaddr.GetSymbolContext(eSymbolContextFunction
  468. | eSymbolContextLineEntry));
  469. if (sc.IsValid()) {
  470. // TODO:need to check if the vm_offset is belong to
  471. SBFunction function(sc.GetFunction());
  472. if (function.IsValid()) {
  473. uint64_t start = func_ctx->aot_func->code
  474. - comp_ctx->comp_data->wasm_module->buf_code;
  475. uint64_t end = func_ctx->aot_func->code
  476. - comp_ctx->comp_data->wasm_module->buf_code
  477. + func_ctx->aot_func->code_size;
  478. if (function.GetStartAddress().GetOffset() <= start
  479. && end <= function.GetEndAddress().GetOffset()) {
  480. auto line_entry = sc.GetLineEntry();
  481. location_info = LLVMDIBuilderCreateDebugLocation(
  482. comp_ctx->context, line_entry.GetLine(),
  483. line_entry.GetColumn(), func_ctx->debug_func, NULL);
  484. // LOG_VERBOSE("Gen the location l:%d, c:%d at %lx",
  485. // line_entry.GetLine(), line_entry.GetColumn(), vm_offset);
  486. }
  487. else
  488. LOG_WARNING("the offset and function is not matched");
  489. }
  490. }
  491. return location_info;
  492. }
  493. LLVMMetadataRef
  494. dwarf_gen_func_ret_location(const AOTCompContext *comp_ctx,
  495. const AOTFuncContext *func_ctx)
  496. {
  497. LLVMMetadataRef func_info = NULL;
  498. dwarf_extractor *extractor;
  499. uint64_t vm_offset;
  500. AOTFunc *func = func_ctx->aot_func;
  501. LLVMMetadataRef location_info = NULL;
  502. if (!(extractor = TO_EXTRACTOR(comp_ctx->comp_data->extractor)))
  503. return NULL;
  504. // A code address in DWARF for WebAssembly is the offset of an
  505. // instruction relative within the Code section of the WebAssembly file.
  506. // For this reason Section::GetFileAddress() must return zero for the
  507. // Code section. (refer to ObjectFileWasm.cpp)
  508. vm_offset = (func->code + func->code_size - 1)
  509. - comp_ctx->comp_data->wasm_module->buf_code;
  510. location_info = dwarf_gen_location(comp_ctx, func_ctx, vm_offset);
  511. return location_info;
  512. }