dwarf_extractor.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 dwar_extractor
  26. {
  27. SBDebugger debugger;
  28. SBTarget target;
  29. SBModule module;
  30. } dwar_extractor;
  31. #define TO_HANDLE(extractor) (dwar_extractor_handle_t)(extractor)
  32. #define TO_EXTACTOR(handle) (dwar_extractor *)(handle)
  33. static bool is_debugger_initialized;
  34. dwar_extractor_handle_t
  35. create_dwarf_extractor(AOTCompData *comp_data, char * file_name)
  36. {
  37. char *arch = NULL;
  38. char *platform = NULL;
  39. dwar_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 dwar_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(dwar_extractor_handle_t handle)
  82. {
  83. dwar_extractor * extractor = TO_EXTACTOR(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(AOTCompContext *comp_ctx)
  94. {
  95. dwar_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_EXTACTOR(comp_ctx->comp_data->extractor)))
  101. return NULL;
  102. units_number = extractor->module.GetNumCompileUnits();
  103. if (units_number > 0) {
  104. SBCompileUnit compile_unit =
  105. extractor->module.GetCompileUnitAtIndex(0);
  106. auto filespec = compile_unit.GetFileSpec();
  107. file_name = filespec.GetFilename();
  108. dir_name = filespec.GetDirectory();
  109. if (file_name || dir_name) {
  110. file_info = LLVMDIBuilderCreateFile(comp_ctx->debug_builder,
  111. file_name, strlen(file_name),
  112. dir_name, strlen(dir_name));
  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. "ant 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(AOTCompContext *comp_ctx)
  157. {
  158. dwar_extractor *extractor;
  159. int units_number;
  160. LLVMMetadataRef comp_unit = NULL;
  161. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  162. return NULL;
  163. units_number = extractor->module.GetNumCompileUnits();
  164. if (units_number > 0) {
  165. SBCompileUnit compile_unit =
  166. extractor->module.GetCompileUnitAtIndex(0);
  167. auto lang_type = compile_unit.GetLanguage();
  168. comp_unit = LLVMDIBuilderCreateCompileUnit(
  169. comp_ctx->debug_builder, LLDB_TO_LLVM_LANG_TYPE(lang_type),
  170. comp_ctx->debug_file, "ant compiler", 12, 0, NULL, 0, 1, NULL, 0,
  171. LLVMDWARFEmissionFull, 0, 0, 0, "/", 1, "", 0);
  172. }
  173. return comp_unit;
  174. }
  175. bool
  176. dwarf_get_func_info(dwar_extractor_handle_t handle, uint64_t offset)
  177. {
  178. dwar_extractor *extractor = TO_EXTACTOR(handle);
  179. auto sbaddr = extractor->target.ResolveFileAddress(offset);
  180. SBSymbolContext sc(
  181. sbaddr.GetSymbolContext(eSymbolContextFunction));
  182. if (sc.IsValid()) {
  183. SBFunction function(sc.GetFunction());
  184. if (function.IsValid()) {
  185. }
  186. }
  187. }
  188. static LLVMDWARFTypeEncoding
  189. lldb_get_basic_type_encoding(BasicType basic_type)
  190. {
  191. LLVMDWARFTypeEncoding encoding = 0;
  192. switch (basic_type)
  193. {
  194. case eBasicTypeUnsignedChar:
  195. encoding = llvm::dwarf::DW_ATE_unsigned_char;
  196. break;
  197. case eBasicTypeSignedChar:
  198. encoding = llvm::dwarf::DW_ATE_signed_char;
  199. break;
  200. case eBasicTypeUnsignedInt:
  201. case eBasicTypeUnsignedLong:
  202. case eBasicTypeUnsignedLongLong:
  203. case eBasicTypeUnsignedWChar:
  204. case eBasicTypeUnsignedInt128:
  205. case eBasicTypeUnsignedShort:
  206. encoding = llvm::dwarf::DW_ATE_unsigned;
  207. break;
  208. case eBasicTypeInt:
  209. case eBasicTypeLong:
  210. case eBasicTypeLongLong:
  211. case eBasicTypeWChar:
  212. case eBasicTypeInt128:
  213. case eBasicTypeShort:
  214. encoding = llvm::dwarf::DW_ATE_signed;
  215. break;
  216. case eBasicTypeBool:
  217. encoding = llvm::dwarf::DW_ATE_boolean;
  218. break;
  219. case eBasicTypeHalf:
  220. case eBasicTypeFloat:
  221. case eBasicTypeDouble:
  222. case eBasicTypeLongDouble:
  223. encoding = llvm::dwarf::DW_ATE_float;
  224. break;
  225. default:
  226. break;
  227. }
  228. return encoding;
  229. }
  230. static LLVMMetadataRef
  231. lldb_type_to_type_dbi(AOTCompContext *comp_ctx, SBType &type)
  232. {
  233. LLVMMetadataRef type_info = NULL;
  234. BasicType basic_type = type.GetBasicType();
  235. uint64_t bit_size = type.GetByteSize() * 8;
  236. LLVMDIBuilderRef DIB = comp_ctx->debug_builder;
  237. LLVMDWARFTypeEncoding encoding;
  238. if (basic_type != eBasicTypeInvalid) {
  239. encoding = lldb_get_basic_type_encoding(basic_type);
  240. type_info = LLVMDIBuilderCreateBasicType(
  241. DIB, type.GetName(), strlen(type.GetName()), bit_size, encoding,
  242. LLVMDIFlagZero);
  243. }
  244. else if (type.IsPointerType()) {
  245. SBType pointee_type = type.GetPointeeType();
  246. type_info = LLVMDIBuilderCreatePointerType(
  247. DIB, lldb_type_to_type_dbi(comp_ctx, pointee_type), bit_size, 0, 0,
  248. "", 0);
  249. }
  250. return type_info;
  251. }
  252. static LLVMMetadataRef
  253. lldb_function_to_function_dbi(AOTCompContext *comp_ctx, SBSymbolContext &sc, AOTFuncContext *func_ctx)
  254. {
  255. SBFunction function(sc.GetFunction());
  256. const char *function_name = function.GetName();
  257. const char *link_name = function.GetName();
  258. SBTypeList function_args = function.GetType().GetFunctionArgumentTypes();
  259. SBType return_type = function.GetType().GetFunctionReturnType();
  260. const size_t num_function_args = function_args.GetSize();
  261. dwar_extractor *extractor;
  262. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  263. return NULL;
  264. LLVMDIBuilderRef DIB = comp_ctx->debug_builder;
  265. LLVMMetadataRef File = comp_ctx->debug_file;
  266. LLVMMetadataRef ParamTypes[num_function_args + 1];
  267. ParamTypes[0] = lldb_type_to_type_dbi(comp_ctx, return_type);
  268. for (uint32_t function_arg_idx = 0; function_arg_idx < num_function_args;
  269. ++function_arg_idx) {
  270. SBType function_arg_type =
  271. function_args.GetTypeAtIndex(function_arg_idx);
  272. if (function_arg_type.IsValid()) {
  273. ParamTypes[function_arg_idx + 1] = lldb_type_to_type_dbi(comp_ctx, function_arg_type);
  274. }
  275. }
  276. LLVMMetadataRef FunctionTy =
  277. LLVMDIBuilderCreateSubroutineType(DIB, File, ParamTypes, num_function_args + 1, LLVMDIFlagZero);
  278. auto line_entry = sc.GetLineEntry();
  279. LLVMMetadataRef ReplaceableFunctionMetadata =
  280. LLVMDIBuilderCreateReplaceableCompositeType(
  281. DIB, 0x15, function_name, strlen(function_name), File, File,
  282. line_entry.GetLine(), 0, 0, 0, LLVMDIFlagFwdDecl, "", 0);
  283. LLVMMetadataRef FunctionMetadata =
  284. LLVMDIBuilderCreateFunction(DIB, File, function_name, strlen(function_name), link_name, strlen(link_name),
  285. File, line_entry.GetLine(), FunctionTy, true, true, line_entry.GetLine(), LLVMDIFlagZero, false);
  286. LLVMMetadataReplaceAllUsesWith(ReplaceableFunctionMetadata, FunctionMetadata);
  287. LLVMSetSubprogram(func_ctx->func, FunctionMetadata);
  288. LLVMMetadataRef ParamExpression = LLVMDIBuilderCreateExpression(DIB, NULL, 0);
  289. auto variable_list = function.GetBlock().GetVariables(extractor->target, true, false,false);
  290. if (num_function_args != variable_list.GetSize())
  291. {
  292. LOG_ERROR("function args number dismatch!:value number=%d, function args=%d", variable_list.GetSize(), num_function_args);
  293. }
  294. LLVMMetadataRef ParamLocation = LLVMDIBuilderCreateDebugLocation(
  295. comp_ctx->context, line_entry.GetLine(), 0, FunctionMetadata, NULL);
  296. //TODO:change to void * or WasmExenv * ?
  297. LLVMMetadataRef voidtype = LLVMDIBuilderCreateBasicType(DIB, "void", 4, 0, 0, LLVMDIFlagZero);
  298. LLVMMetadataRef voidpionter = LLVMDIBuilderCreatePointerType(DIB, voidtype, 64, 0, 0, "void *", 6);
  299. LLVMMetadataRef ParamVar = LLVMDIBuilderCreateParameterVariable(
  300. DIB, FunctionMetadata, "exenv",
  301. 5, 1,
  302. File, //starts form 1, and 1 is exenv,
  303. line_entry.GetLine(), voidpionter, true,
  304. LLVMDIFlagZero);
  305. LLVMValueRef Param =
  306. LLVMGetParam(func_ctx->func, 0);
  307. LLVMBasicBlockRef block_curr =
  308. LLVMGetEntryBasicBlock(func_ctx->func);
  309. LLVMDIBuilderInsertDbgValueAtEnd(DIB, Param, ParamVar,
  310. ParamExpression, ParamLocation,
  311. block_curr);
  312. for (uint32_t function_arg_idx = 0; function_arg_idx < variable_list.GetSize();
  313. ++function_arg_idx) {
  314. SBValue variable(variable_list.GetValueAtIndex(function_arg_idx));
  315. if (variable.IsValid()) {
  316. SBDeclaration dec(variable.GetDeclaration());
  317. auto valtype = variable.GetType();
  318. LLVMMetadataRef ParamLocation = LLVMDIBuilderCreateDebugLocation(
  319. comp_ctx->context, dec.GetLine(), dec.GetColumn(),
  320. FunctionMetadata, NULL);
  321. LLVMMetadataRef ParamVar = LLVMDIBuilderCreateParameterVariable(
  322. DIB, FunctionMetadata, variable.GetName(),
  323. strlen(variable.GetName()), function_arg_idx + 1 + 1,
  324. File, //starts form 1, and 1 is exenv,
  325. dec.GetLine(), ParamTypes[function_arg_idx + 1], true,
  326. LLVMDIFlagZero);
  327. LLVMValueRef Param =
  328. LLVMGetParam(func_ctx->func, function_arg_idx + 1);
  329. LLVMDIBuilderInsertDbgValueAtEnd(DIB, Param, ParamVar,
  330. ParamExpression, ParamLocation,
  331. block_curr);
  332. }
  333. }
  334. return FunctionMetadata;
  335. }
  336. LLVMMetadataRef
  337. dwarf_gen_func_info(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  338. {
  339. LLVMMetadataRef func_info = NULL;
  340. dwar_extractor *extractor;
  341. uint64_t vm_offset;
  342. AOTFunc *func = func_ctx->aot_func;
  343. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  344. return NULL;
  345. // A code address in DWARF for WebAssembly is the offset of an
  346. // instruction relative within the Code section of the WebAssembly file.
  347. // For this reason Section::GetFileAddress() must return zero for the
  348. // Code section. (refert to ObjectFileWasm.cpp)
  349. vm_offset = func->code - comp_ctx->comp_data->wasm_module->buf_code;
  350. auto sbaddr = extractor->target.ResolveFileAddress(vm_offset);
  351. SBSymbolContext sc(
  352. sbaddr.GetSymbolContext(eSymbolContextFunction | eSymbolContextLineEntry));
  353. if (sc.IsValid()) {
  354. SBFunction function(sc.GetFunction());
  355. if (function.IsValid()) {
  356. func_info = lldb_function_to_function_dbi(comp_ctx, sc, func_ctx);
  357. }
  358. }
  359. return func_info;
  360. }
  361. void
  362. dwarf_get_func_name(AOTCompContext *comp_ctx,
  363. AOTFuncContext *func_ctx,
  364. char *name,
  365. int len)
  366. {
  367. LLVMMetadataRef func_info = NULL;
  368. dwar_extractor *extractor;
  369. uint64_t vm_offset;
  370. AOTFunc *func = func_ctx->aot_func;
  371. name[0] = '\0';
  372. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  373. return ;
  374. // A code address in DWARF for WebAssembly is the offset of an
  375. // instruction relative within the Code section of the WebAssembly file.
  376. // For this reason Section::GetFileAddress() must return zero for the
  377. // Code section. (refert to ObjectFileWasm.cpp)
  378. vm_offset = func->code - comp_ctx->comp_data->wasm_module->buf_code;
  379. auto sbaddr = extractor->target.ResolveFileAddress(vm_offset);
  380. SBSymbolContext sc(sbaddr.GetSymbolContext(eSymbolContextFunction
  381. | eSymbolContextLineEntry));
  382. if (sc.IsValid()) {
  383. SBFunction function(sc.GetFunction());
  384. if (function.IsValid()) {
  385. bh_strcpy_s(name, len, function.GetName());
  386. }
  387. }
  388. }
  389. LLVMMetadataRef
  390. dwarf_gen_location(AOTCompContext *comp_ctx,
  391. AOTFuncContext *func_ctx,
  392. uint64_t vm_offset)
  393. {
  394. LLVMMetadataRef location_info = NULL;
  395. dwar_extractor *extractor;
  396. AOTFunc *func = func_ctx->aot_func;
  397. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  398. return NULL;
  399. auto sbaddr = extractor->target.ResolveFileAddress(vm_offset);
  400. SBSymbolContext sc(sbaddr.GetSymbolContext(eSymbolContextFunction
  401. | eSymbolContextLineEntry));
  402. if (sc.IsValid()) {
  403. //TODO:need to check if the vm_offset is belong to
  404. SBFunction function(sc.GetFunction());
  405. if (function.IsValid()) {
  406. uint64_t start = func_ctx->aot_func->code
  407. - comp_ctx->comp_data->wasm_module->buf_code;
  408. uint64_t end = func_ctx->aot_func->code
  409. - comp_ctx->comp_data->wasm_module->buf_code
  410. + func_ctx->aot_func->code_size;
  411. if (function.GetStartAddress().GetOffset() <= start
  412. && end <= function.GetEndAddress().GetOffset()) {
  413. auto line_entry = sc.GetLineEntry();
  414. location_info =
  415. LLVMDIBuilderCreateDebugLocation(
  416. comp_ctx->context, line_entry.GetLine(),
  417. line_entry.GetColumn(), func_ctx->debug_func, NULL);
  418. //LOG_VERBOSE("Gen the location l:%d, c:%d at %lx", line_entry.GetLine(), line_entry.GetColumn(), vm_offset);
  419. } else
  420. LOG_WARNING("the offset and function is not matched");
  421. }
  422. }
  423. return location_info;
  424. }
  425. LLVMMetadataRef
  426. dwarf_gen_func_ret_location(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  427. {
  428. LLVMMetadataRef func_info = NULL;
  429. dwar_extractor *extractor;
  430. uint64_t vm_offset;
  431. AOTFunc *func = func_ctx->aot_func;
  432. LLVMMetadataRef location_info = NULL;
  433. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  434. return NULL;
  435. // A code address in DWARF for WebAssembly is the offset of an
  436. // instruction relative within the Code section of the WebAssembly file.
  437. // For this reason Section::GetFileAddress() must return zero for the
  438. // Code section. (refert to ObjectFileWasm.cpp)
  439. vm_offset = (func->code + func->code_size -1) - comp_ctx->comp_data->wasm_module->buf_code;
  440. location_info = dwarf_gen_location(comp_ctx, func_ctx, vm_offset);
  441. return location_info;
  442. }