dwarf_extractor.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. SBDebugger debugger;
  27. SBTarget target;
  28. SBModule module;
  29. } dwar_extractor;
  30. #define TO_HANDLE(extractor) (dwar_extractor_handle_t)(extractor)
  31. #define TO_EXTACTOR(handle) (dwar_extractor *)(handle)
  32. static bool is_debugger_initialized;
  33. dwar_extractor_handle_t
  34. create_dwarf_extractor(AOTCompData *comp_data, char *file_name)
  35. {
  36. char *arch = NULL;
  37. char *platform = NULL;
  38. dwar_extractor *extractor = NULL;
  39. //__attribute__((constructor)) may be better?
  40. if (!is_debugger_initialized) {
  41. SBError error = SBDebugger::InitializeWithErrorHandling();
  42. if (error.Fail()) {
  43. LOG_ERROR("Init Dwarf Debugger failed");
  44. return TO_HANDLE(NULL);
  45. }
  46. is_debugger_initialized = true;
  47. }
  48. SBError error;
  49. SBFileSpec exe_file_spec(file_name, true);
  50. if (!(extractor = new dwar_extractor())) {
  51. LOG_ERROR("Create Dwarf Extractor error: failed to allocate memory");
  52. goto fail3;
  53. }
  54. extractor->debugger = SBDebugger::Create();
  55. if (!extractor->debugger.IsValid()) {
  56. LOG_ERROR("Create Dwarf Debugger failed");
  57. goto fail2;
  58. }
  59. extractor->target = extractor->debugger.CreateTarget(
  60. file_name, arch, platform, false, error);
  61. if (!error.Success()) {
  62. LOG_ERROR("Create Dwarf target failed:%s", error.GetCString());
  63. goto fail1;
  64. }
  65. if (!extractor->target.IsValid()) {
  66. LOG_ERROR("Create Dwarf target not valid");
  67. goto fail1;
  68. }
  69. extractor->module = extractor->target.FindModule(exe_file_spec);
  70. comp_data->extractor = TO_HANDLE(extractor);
  71. return TO_HANDLE(extractor);
  72. fail1:
  73. SBDebugger::Destroy(extractor->debugger);
  74. fail2:
  75. wasm_runtime_free(extractor);
  76. fail3:
  77. return TO_HANDLE(NULL);
  78. }
  79. void
  80. destroy_dwarf_extractor(dwar_extractor_handle_t handle)
  81. {
  82. dwar_extractor *extractor = TO_EXTACTOR(handle);
  83. if (!extractor)
  84. return;
  85. extractor->debugger.DeleteTarget(extractor->target);
  86. SBDebugger::Destroy(extractor->debugger);
  87. delete extractor;
  88. SBDebugger::Terminate();
  89. is_debugger_initialized = false;
  90. }
  91. LLVMMetadataRef
  92. dwarf_gen_file_info(AOTCompContext *comp_ctx)
  93. {
  94. dwar_extractor *extractor;
  95. int units_number;
  96. LLVMMetadataRef file_info = NULL;
  97. const char *file_name;
  98. const char *dir_name;
  99. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  100. return NULL;
  101. units_number = extractor->module.GetNumCompileUnits();
  102. if (units_number > 0) {
  103. SBCompileUnit compile_unit = extractor->module.GetCompileUnitAtIndex(0);
  104. auto filespec = compile_unit.GetFileSpec();
  105. file_name = filespec.GetFilename();
  106. dir_name = filespec.GetDirectory();
  107. if (file_name || dir_name) {
  108. file_info = LLVMDIBuilderCreateFile(comp_ctx->debug_builder,
  109. file_name, strlen(file_name),
  110. dir_name, strlen(dir_name));
  111. }
  112. }
  113. return file_info;
  114. }
  115. #if 0
  116. void
  117. dwarf_gen_mock_vm_info(AOTCompContext *comp_ctx)
  118. {
  119. LLVMMetadataRef file_info = NULL;
  120. LLVMMetadataRef comp_unit = NULL;
  121. file_info = LLVMDIBuilderCreateFile(comp_ctx->debug_builder,
  122. "ant_runtime_mock.c", 18, ".", 1);
  123. comp_unit = LLVMDIBuilderCreateCompileUnit(
  124. comp_ctx->debug_builder, LLVMDWARFSourceLanguageC, file_info,
  125. "ant compiler", 12, 0, NULL, 0, 1, NULL, 0, LLVMDWARFEmissionFull, 0, 0,
  126. 0, "/", 1, "", 0);
  127. LLVMTypeRef ParamTys[] = {
  128. LLVMVoidType(),
  129. };
  130. LLVMTypeRef FuncTy = LLVMFunctionType(LLVMVoidType(), ParamTys, 0, 0);
  131. LLVMValueRef Function =
  132. LLVMAddFunction(comp_ctx->module, "ant_runtime_mock", FuncTy);
  133. LLVMMetadataRef ParamTypes[0];
  134. LLVMMetadataRef FunctionTy = LLVMDIBuilderCreateSubroutineType(
  135. comp_ctx->debug_builder, file_info, ParamTypes, 0, LLVMDIFlagZero);
  136. /* 0x0015 is subroutine_type */
  137. LLVMMetadataRef ReplaceableFunctionMetadata =
  138. LLVMDIBuilderCreateReplaceableCompositeType(
  139. comp_ctx->debug_builder, 0x15, "ant_runtime_mock", 16, file_info,
  140. file_info, 2, 0, 0, 0, LLVMDIFlagFwdDecl, "", 0);
  141. LLVMMetadataRef FunctionMetadata = LLVMDIBuilderCreateFunction(
  142. comp_ctx->debug_builder, file_info, "ant_runtime_mock", 16,
  143. "ant_runtime_mock", 16, file_info, 2, FunctionTy, true, true, 2, LLVMDIFlagZero,
  144. false);
  145. LLVMMetadataReplaceAllUsesWith(ReplaceableFunctionMetadata,
  146. FunctionMetadata);
  147. LLVMSetSubprogram(Function, FunctionMetadata);
  148. comp_ctx->vm_debug_comp_unit = comp_unit;
  149. comp_ctx->vm_debug_file = file_info;
  150. comp_ctx->vm_debug_func = FunctionMetadata;
  151. }
  152. #endif
  153. LLVMMetadataRef
  154. dwarf_gen_comp_unit_info(AOTCompContext *comp_ctx)
  155. {
  156. dwar_extractor *extractor;
  157. int units_number;
  158. LLVMMetadataRef comp_unit = NULL;
  159. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  160. return NULL;
  161. units_number = extractor->module.GetNumCompileUnits();
  162. if (units_number > 0) {
  163. SBCompileUnit compile_unit = extractor->module.GetCompileUnitAtIndex(0);
  164. auto lang_type = compile_unit.GetLanguage();
  165. comp_unit = LLVMDIBuilderCreateCompileUnit(
  166. comp_ctx->debug_builder, LLDB_TO_LLVM_LANG_TYPE(lang_type),
  167. comp_ctx->debug_file, "ant compiler", 12, 0, NULL, 0, 1, NULL, 0,
  168. LLVMDWARFEmissionFull, 0, 0, 0, "/", 1, "", 0);
  169. }
  170. return comp_unit;
  171. }
  172. bool
  173. dwarf_get_func_info(dwar_extractor_handle_t handle, uint64_t offset)
  174. {
  175. dwar_extractor *extractor = TO_EXTACTOR(handle);
  176. auto sbaddr = extractor->target.ResolveFileAddress(offset);
  177. SBSymbolContext sc(sbaddr.GetSymbolContext(eSymbolContextFunction));
  178. if (sc.IsValid()) {
  179. SBFunction function(sc.GetFunction());
  180. if (function.IsValid()) {
  181. }
  182. }
  183. }
  184. static LLVMDWARFTypeEncoding
  185. lldb_get_basic_type_encoding(BasicType basic_type)
  186. {
  187. LLVMDWARFTypeEncoding encoding = 0;
  188. switch (basic_type) {
  189. case eBasicTypeUnsignedChar:
  190. encoding = llvm::dwarf::DW_ATE_unsigned_char;
  191. break;
  192. case eBasicTypeSignedChar:
  193. encoding = llvm::dwarf::DW_ATE_signed_char;
  194. break;
  195. case eBasicTypeUnsignedInt:
  196. case eBasicTypeUnsignedLong:
  197. case eBasicTypeUnsignedLongLong:
  198. case eBasicTypeUnsignedWChar:
  199. case eBasicTypeUnsignedInt128:
  200. case eBasicTypeUnsignedShort:
  201. encoding = llvm::dwarf::DW_ATE_unsigned;
  202. break;
  203. case eBasicTypeInt:
  204. case eBasicTypeLong:
  205. case eBasicTypeLongLong:
  206. case eBasicTypeWChar:
  207. case eBasicTypeInt128:
  208. case eBasicTypeShort:
  209. encoding = llvm::dwarf::DW_ATE_signed;
  210. break;
  211. case eBasicTypeBool:
  212. encoding = llvm::dwarf::DW_ATE_boolean;
  213. break;
  214. case eBasicTypeHalf:
  215. case eBasicTypeFloat:
  216. case eBasicTypeDouble:
  217. case eBasicTypeLongDouble:
  218. encoding = llvm::dwarf::DW_ATE_float;
  219. break;
  220. default:
  221. break;
  222. }
  223. return encoding;
  224. }
  225. static LLVMMetadataRef
  226. lldb_type_to_type_dbi(AOTCompContext *comp_ctx, SBType &type)
  227. {
  228. LLVMMetadataRef type_info = NULL;
  229. BasicType basic_type = type.GetBasicType();
  230. uint64_t bit_size = type.GetByteSize() * 8;
  231. LLVMDIBuilderRef DIB = comp_ctx->debug_builder;
  232. LLVMDWARFTypeEncoding encoding;
  233. if (basic_type != eBasicTypeInvalid) {
  234. encoding = lldb_get_basic_type_encoding(basic_type);
  235. type_info = LLVMDIBuilderCreateBasicType(
  236. DIB, type.GetName(), strlen(type.GetName()), bit_size, encoding,
  237. LLVMDIFlagZero);
  238. }
  239. else if (type.IsPointerType()) {
  240. SBType pointee_type = type.GetPointeeType();
  241. type_info = LLVMDIBuilderCreatePointerType(
  242. DIB, lldb_type_to_type_dbi(comp_ctx, pointee_type), bit_size, 0, 0,
  243. "", 0);
  244. }
  245. return type_info;
  246. }
  247. static LLVMMetadataRef
  248. lldb_function_to_function_dbi(AOTCompContext *comp_ctx, SBSymbolContext &sc,
  249. AOTFuncContext *func_ctx)
  250. {
  251. SBFunction function(sc.GetFunction());
  252. const char *function_name = function.GetName();
  253. const char *link_name = function.GetName();
  254. SBTypeList function_args = function.GetType().GetFunctionArgumentTypes();
  255. SBType return_type = function.GetType().GetFunctionReturnType();
  256. const size_t num_function_args = function_args.GetSize();
  257. dwar_extractor *extractor;
  258. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  259. return NULL;
  260. LLVMDIBuilderRef DIB = comp_ctx->debug_builder;
  261. LLVMMetadataRef File = comp_ctx->debug_file;
  262. LLVMMetadataRef ParamTypes[num_function_args + 1];
  263. ParamTypes[0] = lldb_type_to_type_dbi(comp_ctx, return_type);
  264. for (uint32_t function_arg_idx = 0; function_arg_idx < num_function_args;
  265. ++function_arg_idx) {
  266. SBType function_arg_type =
  267. function_args.GetTypeAtIndex(function_arg_idx);
  268. if (function_arg_type.IsValid()) {
  269. ParamTypes[function_arg_idx + 1] =
  270. lldb_type_to_type_dbi(comp_ctx, function_arg_type);
  271. }
  272. }
  273. LLVMMetadataRef FunctionTy = LLVMDIBuilderCreateSubroutineType(
  274. DIB, File, ParamTypes, num_function_args + 1, LLVMDIFlagZero);
  275. auto line_entry = sc.GetLineEntry();
  276. LLVMMetadataRef ReplaceableFunctionMetadata =
  277. LLVMDIBuilderCreateReplaceableCompositeType(
  278. DIB, 0x15, function_name, strlen(function_name), File, File,
  279. line_entry.GetLine(), 0, 0, 0, LLVMDIFlagFwdDecl, "", 0);
  280. LLVMMetadataRef FunctionMetadata = LLVMDIBuilderCreateFunction(
  281. DIB, File, function_name, strlen(function_name), link_name,
  282. strlen(link_name), File, line_entry.GetLine(), FunctionTy, true, true,
  283. line_entry.GetLine(), LLVMDIFlagZero, false);
  284. LLVMMetadataReplaceAllUsesWith(ReplaceableFunctionMetadata,
  285. FunctionMetadata);
  286. LLVMSetSubprogram(func_ctx->func, FunctionMetadata);
  287. LLVMMetadataRef ParamExpression =
  288. LLVMDIBuilderCreateExpression(DIB, NULL, 0);
  289. auto variable_list =
  290. function.GetBlock().GetVariables(extractor->target, true, false, false);
  291. if (num_function_args != variable_list.GetSize()) {
  292. LOG_ERROR(
  293. "function args number dismatch!:value number=%d, function args=%d",
  294. variable_list.GetSize(), num_function_args);
  295. }
  296. LLVMMetadataRef ParamLocation = LLVMDIBuilderCreateDebugLocation(
  297. comp_ctx->context, line_entry.GetLine(), 0, FunctionMetadata, NULL);
  298. // TODO:change to void * or WasmExenv * ?
  299. LLVMMetadataRef voidtype =
  300. LLVMDIBuilderCreateBasicType(DIB, "void", 4, 0, 0, LLVMDIFlagZero);
  301. LLVMMetadataRef voidpionter =
  302. LLVMDIBuilderCreatePointerType(DIB, voidtype, 64, 0, 0, "void *", 6);
  303. LLVMMetadataRef ParamVar = LLVMDIBuilderCreateParameterVariable(
  304. DIB, FunctionMetadata, "exenv", 5, 1,
  305. File, // starts form 1, and 1 is exenv,
  306. line_entry.GetLine(), voidpionter, true, LLVMDIFlagZero);
  307. LLVMValueRef Param = LLVMGetParam(func_ctx->func, 0);
  308. LLVMBasicBlockRef block_curr = LLVMGetEntryBasicBlock(func_ctx->func);
  309. LLVMDIBuilderInsertDbgValueAtEnd(DIB, Param, ParamVar, ParamExpression,
  310. ParamLocation, block_curr);
  311. for (uint32_t function_arg_idx = 0;
  312. function_arg_idx < variable_list.GetSize(); ++function_arg_idx) {
  313. SBValue variable(variable_list.GetValueAtIndex(function_arg_idx));
  314. if (variable.IsValid()) {
  315. SBDeclaration dec(variable.GetDeclaration());
  316. auto valtype = variable.GetType();
  317. LLVMMetadataRef ParamLocation = LLVMDIBuilderCreateDebugLocation(
  318. comp_ctx->context, dec.GetLine(), dec.GetColumn(),
  319. FunctionMetadata, NULL);
  320. LLVMMetadataRef ParamVar = LLVMDIBuilderCreateParameterVariable(
  321. DIB, FunctionMetadata, variable.GetName(),
  322. strlen(variable.GetName()), function_arg_idx + 1 + 1,
  323. File, // starts form 1, and 1 is exenv,
  324. dec.GetLine(), ParamTypes[function_arg_idx + 1], true,
  325. LLVMDIFlagZero);
  326. LLVMValueRef Param =
  327. LLVMGetParam(func_ctx->func, function_arg_idx + 1);
  328. LLVMDIBuilderInsertDbgValueAtEnd(DIB, Param, ParamVar,
  329. ParamExpression, ParamLocation,
  330. block_curr);
  331. }
  332. }
  333. return FunctionMetadata;
  334. }
  335. LLVMMetadataRef
  336. dwarf_gen_func_info(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  337. {
  338. LLVMMetadataRef func_info = NULL;
  339. dwar_extractor *extractor;
  340. uint64_t vm_offset;
  341. AOTFunc *func = func_ctx->aot_func;
  342. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  343. return NULL;
  344. // A code address in DWARF for WebAssembly is the offset of an
  345. // instruction relative within the Code section of the WebAssembly file.
  346. // For this reason Section::GetFileAddress() must return zero for the
  347. // Code section. (refert to ObjectFileWasm.cpp)
  348. vm_offset = func->code - comp_ctx->comp_data->wasm_module->buf_code;
  349. auto sbaddr = extractor->target.ResolveFileAddress(vm_offset);
  350. SBSymbolContext sc(sbaddr.GetSymbolContext(eSymbolContextFunction
  351. | eSymbolContextLineEntry));
  352. if (sc.IsValid()) {
  353. SBFunction function(sc.GetFunction());
  354. if (function.IsValid()) {
  355. func_info = lldb_function_to_function_dbi(comp_ctx, sc, func_ctx);
  356. }
  357. }
  358. return func_info;
  359. }
  360. void
  361. dwarf_get_func_name(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  362. char *name, int len)
  363. {
  364. LLVMMetadataRef func_info = NULL;
  365. dwar_extractor *extractor;
  366. uint64_t vm_offset;
  367. AOTFunc *func = func_ctx->aot_func;
  368. name[0] = '\0';
  369. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  370. return;
  371. // A code address in DWARF for WebAssembly is the offset of an
  372. // instruction relative within the Code section of the WebAssembly file.
  373. // For this reason Section::GetFileAddress() must return zero for the
  374. // Code section. (refert to ObjectFileWasm.cpp)
  375. vm_offset = func->code - comp_ctx->comp_data->wasm_module->buf_code;
  376. auto sbaddr = extractor->target.ResolveFileAddress(vm_offset);
  377. SBSymbolContext sc(sbaddr.GetSymbolContext(eSymbolContextFunction
  378. | eSymbolContextLineEntry));
  379. if (sc.IsValid()) {
  380. SBFunction function(sc.GetFunction());
  381. if (function.IsValid()) {
  382. bh_strcpy_s(name, len, function.GetName());
  383. }
  384. }
  385. }
  386. LLVMMetadataRef
  387. dwarf_gen_location(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  388. uint64_t vm_offset)
  389. {
  390. LLVMMetadataRef location_info = NULL;
  391. dwar_extractor *extractor;
  392. AOTFunc *func = func_ctx->aot_func;
  393. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  394. return NULL;
  395. auto sbaddr = extractor->target.ResolveFileAddress(vm_offset);
  396. SBSymbolContext sc(sbaddr.GetSymbolContext(eSymbolContextFunction
  397. | eSymbolContextLineEntry));
  398. if (sc.IsValid()) {
  399. // TODO:need to check if the vm_offset is belong to
  400. SBFunction function(sc.GetFunction());
  401. if (function.IsValid()) {
  402. uint64_t start = func_ctx->aot_func->code
  403. - comp_ctx->comp_data->wasm_module->buf_code;
  404. uint64_t end = func_ctx->aot_func->code
  405. - comp_ctx->comp_data->wasm_module->buf_code
  406. + func_ctx->aot_func->code_size;
  407. if (function.GetStartAddress().GetOffset() <= start
  408. && end <= function.GetEndAddress().GetOffset()) {
  409. auto line_entry = sc.GetLineEntry();
  410. location_info = LLVMDIBuilderCreateDebugLocation(
  411. comp_ctx->context, line_entry.GetLine(),
  412. line_entry.GetColumn(), func_ctx->debug_func, NULL);
  413. // LOG_VERBOSE("Gen the location l:%d, c:%d at %lx",
  414. // line_entry.GetLine(), line_entry.GetColumn(), vm_offset);
  415. }
  416. else
  417. LOG_WARNING("the offset and function is not matched");
  418. }
  419. }
  420. return location_info;
  421. }
  422. LLVMMetadataRef
  423. dwarf_gen_func_ret_location(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  424. {
  425. LLVMMetadataRef func_info = NULL;
  426. dwar_extractor *extractor;
  427. uint64_t vm_offset;
  428. AOTFunc *func = func_ctx->aot_func;
  429. LLVMMetadataRef location_info = NULL;
  430. if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
  431. return NULL;
  432. // A code address in DWARF for WebAssembly is the offset of an
  433. // instruction relative within the Code section of the WebAssembly file.
  434. // For this reason Section::GetFileAddress() must return zero for the
  435. // Code section. (refert to ObjectFileWasm.cpp)
  436. vm_offset = (func->code + func->code_size - 1)
  437. - comp_ctx->comp_data->wasm_module->buf_code;
  438. location_info = dwarf_gen_location(comp_ctx, func_ctx, vm_offset);
  439. return location_info;
  440. }