summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/compilation/debug/dwarf_extractor.cpp
blob: d5a1be85e06254f1ab9700048f718f090273b61b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
 * Copyright (C) 2021 Ant Group.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "lldb/API/SBBlock.h"
#include "lldb/API/SBCompileUnit.h"
#include "lldb/API/SBCommandReturnObject.h"
#include "lldb/API/SBCommandInterpreter.h"
#include "lldb/API/SBBreakpointLocation.h"
#include "lldb/API/SBDebugger.h"
#include "lldb/API//SBFunction.h"
#include "lldb/API//SBModule.h"
#include "lldb/API//SBProcess.h"
#include "lldb/API//SBStream.h"
#include "lldb/API//SBSymbol.h"
#include "lldb/API//SBTarget.h"
#include "lldb/API//SBThread.h"
#include "lldb/API/SBDeclaration.h"

#include "dwarf_extractor.h"
#include "../aot_llvm.h"

#include "bh_log.h"
#include "../../aot/aot_runtime.h"

#include "llvm/BinaryFormat/Dwarf.h"

using namespace lldb;

typedef struct dwar_extractor {
    SBDebugger debugger;
    SBTarget target;
    SBModule module;

} dwar_extractor;

#define TO_HANDLE(extractor) (dwar_extractor_handle_t)(extractor)

#define TO_EXTACTOR(handle) (dwar_extractor *)(handle)

static bool is_debugger_initialized;

dwar_extractor_handle_t
create_dwarf_extractor(AOTCompData *comp_data, char *file_name)
{
    char *arch = NULL;
    char *platform = NULL;
    dwar_extractor *extractor = NULL;

    //__attribute__((constructor)) may be better?
    if (!is_debugger_initialized) {
        SBError error = SBDebugger::InitializeWithErrorHandling();
        if (error.Fail()) {
            LOG_ERROR("Init Dwarf Debugger failed");
            return TO_HANDLE(NULL);
        }
        is_debugger_initialized = true;
    }

    SBError error;
    SBFileSpec exe_file_spec(file_name, true);

    if (!(extractor = new dwar_extractor())) {
        LOG_ERROR("Create Dwarf Extractor error: failed to allocate memory");
        goto fail3;
    }

    extractor->debugger = SBDebugger::Create();
    if (!extractor->debugger.IsValid()) {
        LOG_ERROR("Create Dwarf Debugger failed");
        goto fail2;
    }

    extractor->target = extractor->debugger.CreateTarget(
        file_name, arch, platform, false, error);

    if (!error.Success()) {
        LOG_ERROR("Create Dwarf target failed:%s", error.GetCString());
        goto fail1;
    }

    if (!extractor->target.IsValid()) {
        LOG_ERROR("Create Dwarf target not valid");
        goto fail1;
    }

    extractor->module = extractor->target.FindModule(exe_file_spec);
    comp_data->extractor = TO_HANDLE(extractor);

    return TO_HANDLE(extractor);

fail1:
    SBDebugger::Destroy(extractor->debugger);

fail2:
    wasm_runtime_free(extractor);

fail3:
    return TO_HANDLE(NULL);
}

void
destroy_dwarf_extractor(dwar_extractor_handle_t handle)
{
    dwar_extractor *extractor = TO_EXTACTOR(handle);
    if (!extractor)
        return;
    extractor->debugger.DeleteTarget(extractor->target);
    SBDebugger::Destroy(extractor->debugger);
    delete extractor;
    SBDebugger::Terminate();
    is_debugger_initialized = false;
}

LLVMMetadataRef
dwarf_gen_file_info(AOTCompContext *comp_ctx)
{
    dwar_extractor *extractor;
    int units_number;
    LLVMMetadataRef file_info = NULL;
    const char *file_name;
    const char *dir_name;

    if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
        return NULL;

    units_number = extractor->module.GetNumCompileUnits();

    if (units_number > 0) {
        SBCompileUnit compile_unit = extractor->module.GetCompileUnitAtIndex(0);
        auto filespec = compile_unit.GetFileSpec();
        file_name = filespec.GetFilename();
        dir_name = filespec.GetDirectory();
        if (file_name || dir_name) {
            file_info = LLVMDIBuilderCreateFile(comp_ctx->debug_builder,
                                                file_name, strlen(file_name),
                                                dir_name, strlen(dir_name));
        }
    }
    return file_info;
}

#if 0
void
dwarf_gen_mock_vm_info(AOTCompContext *comp_ctx)
{
    LLVMMetadataRef file_info = NULL;
    LLVMMetadataRef comp_unit = NULL;
    file_info = LLVMDIBuilderCreateFile(comp_ctx->debug_builder,
                                        "ant_runtime_mock.c", 18, ".", 1);

    comp_unit = LLVMDIBuilderCreateCompileUnit(
      comp_ctx->debug_builder, LLVMDWARFSourceLanguageC, file_info,
      "ant compiler", 12, 0, NULL, 0, 1, NULL, 0, LLVMDWARFEmissionFull, 0, 0,
      0, "/", 1, "", 0);

    LLVMTypeRef ParamTys[] = {
        LLVMVoidType(),
    };

    LLVMTypeRef FuncTy = LLVMFunctionType(LLVMVoidType(), ParamTys, 0, 0);

    LLVMValueRef Function =
      LLVMAddFunction(comp_ctx->module, "ant_runtime_mock", FuncTy);

    LLVMMetadataRef ParamTypes[0];
    LLVMMetadataRef FunctionTy = LLVMDIBuilderCreateSubroutineType(
      comp_ctx->debug_builder, file_info, ParamTypes, 0, LLVMDIFlagZero);

    /* 0x0015 is subroutine_type */
    LLVMMetadataRef ReplaceableFunctionMetadata =
      LLVMDIBuilderCreateReplaceableCompositeType(
        comp_ctx->debug_builder, 0x15, "ant_runtime_mock", 16, file_info,
        file_info, 2, 0, 0, 0, LLVMDIFlagFwdDecl, "", 0);

    LLVMMetadataRef FunctionMetadata = LLVMDIBuilderCreateFunction(
      comp_ctx->debug_builder, file_info, "ant_runtime_mock", 16,
      "ant_runtime_mock", 16, file_info, 2, FunctionTy, true, true, 2, LLVMDIFlagZero,
      false);

    LLVMMetadataReplaceAllUsesWith(ReplaceableFunctionMetadata,
                                   FunctionMetadata);

    LLVMSetSubprogram(Function, FunctionMetadata);

    comp_ctx->vm_debug_comp_unit = comp_unit;
    comp_ctx->vm_debug_file = file_info;
    comp_ctx->vm_debug_func = FunctionMetadata;
}
#endif

LLVMMetadataRef
dwarf_gen_comp_unit_info(AOTCompContext *comp_ctx)
{
    dwar_extractor *extractor;
    int units_number;
    LLVMMetadataRef comp_unit = NULL;

    if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
        return NULL;

    units_number = extractor->module.GetNumCompileUnits();

    if (units_number > 0) {
        SBCompileUnit compile_unit = extractor->module.GetCompileUnitAtIndex(0);
        auto lang_type = compile_unit.GetLanguage();

        comp_unit = LLVMDIBuilderCreateCompileUnit(
            comp_ctx->debug_builder, LLDB_TO_LLVM_LANG_TYPE(lang_type),
            comp_ctx->debug_file, "ant compiler", 12, 0, NULL, 0, 1, NULL, 0,
            LLVMDWARFEmissionFull, 0, 0, 0, "/", 1, "", 0);
    }
    return comp_unit;
}

static LLVMDWARFTypeEncoding
lldb_get_basic_type_encoding(BasicType basic_type)
{
    LLVMDWARFTypeEncoding encoding = 0;
    switch (basic_type) {
        case eBasicTypeUnsignedChar:
            encoding = llvm::dwarf::DW_ATE_unsigned_char;
            break;
        case eBasicTypeSignedChar:
            encoding = llvm::dwarf::DW_ATE_signed_char;
            break;
        case eBasicTypeUnsignedInt:
        case eBasicTypeUnsignedLong:
        case eBasicTypeUnsignedLongLong:
        case eBasicTypeUnsignedWChar:
        case eBasicTypeUnsignedInt128:
        case eBasicTypeUnsignedShort:
            encoding = llvm::dwarf::DW_ATE_unsigned;
            break;
        case eBasicTypeInt:
        case eBasicTypeLong:
        case eBasicTypeLongLong:
        case eBasicTypeWChar:
        case eBasicTypeInt128:
        case eBasicTypeShort:
            encoding = llvm::dwarf::DW_ATE_signed;
            break;
        case eBasicTypeBool:
            encoding = llvm::dwarf::DW_ATE_boolean;
            break;
        case eBasicTypeHalf:
        case eBasicTypeFloat:
        case eBasicTypeDouble:
        case eBasicTypeLongDouble:
            encoding = llvm::dwarf::DW_ATE_float;
            break;
        default:
            break;
    }
    return encoding;
}

static LLVMMetadataRef
lldb_type_to_type_dbi(AOTCompContext *comp_ctx, SBType &type)
{
    LLVMMetadataRef type_info = NULL;
    BasicType basic_type = type.GetBasicType();
    uint64_t bit_size = type.GetByteSize() * 8;
    LLVMDIBuilderRef DIB = comp_ctx->debug_builder;
    LLVMDWARFTypeEncoding encoding;

    if (basic_type != eBasicTypeInvalid) {
        encoding = lldb_get_basic_type_encoding(basic_type);
        type_info = LLVMDIBuilderCreateBasicType(
            DIB, type.GetName(), strlen(type.GetName()), bit_size, encoding,
            LLVMDIFlagZero);
    }
    else if (type.IsPointerType()) {
        SBType pointee_type = type.GetPointeeType();
        type_info = LLVMDIBuilderCreatePointerType(
            DIB, lldb_type_to_type_dbi(comp_ctx, pointee_type), bit_size, 0, 0,
            "", 0);
    }

    return type_info;
}

static LLVMMetadataRef
lldb_function_to_function_dbi(AOTCompContext *comp_ctx, SBSymbolContext &sc,
                              AOTFuncContext *func_ctx)
{
    SBFunction function(sc.GetFunction());
    const char *function_name = function.GetName();
    const char *link_name = function.GetName();
    SBTypeList function_args = function.GetType().GetFunctionArgumentTypes();
    SBType return_type = function.GetType().GetFunctionReturnType();
    const size_t num_function_args = function_args.GetSize();
    dwar_extractor *extractor;

    if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
        return NULL;

    LLVMDIBuilderRef DIB = comp_ctx->debug_builder;
    LLVMMetadataRef File = comp_ctx->debug_file;

    LLVMMetadataRef ParamTypes[num_function_args + 1];

    ParamTypes[0] = lldb_type_to_type_dbi(comp_ctx, return_type);

    for (uint32_t function_arg_idx = 0; function_arg_idx < num_function_args;
         ++function_arg_idx) {
        SBType function_arg_type =
            function_args.GetTypeAtIndex(function_arg_idx);

        if (function_arg_type.IsValid()) {
            ParamTypes[function_arg_idx + 1] =
                lldb_type_to_type_dbi(comp_ctx, function_arg_type);
        }
    }

    LLVMMetadataRef FunctionTy = LLVMDIBuilderCreateSubroutineType(
        DIB, File, ParamTypes, num_function_args + 1, LLVMDIFlagZero);

    auto line_entry = sc.GetLineEntry();
    LLVMMetadataRef ReplaceableFunctionMetadata =
        LLVMDIBuilderCreateReplaceableCompositeType(
            DIB, 0x15, function_name, strlen(function_name), File, File,
            line_entry.GetLine(), 0, 0, 0, LLVMDIFlagFwdDecl, "", 0);

    LLVMMetadataRef FunctionMetadata = LLVMDIBuilderCreateFunction(
        DIB, File, function_name, strlen(function_name), link_name,
        strlen(link_name), File, line_entry.GetLine(), FunctionTy, true, true,
        line_entry.GetLine(), LLVMDIFlagZero, false);

    LLVMMetadataReplaceAllUsesWith(ReplaceableFunctionMetadata,
                                   FunctionMetadata);

    LLVMSetSubprogram(func_ctx->func, FunctionMetadata);

    LLVMMetadataRef ParamExpression =
        LLVMDIBuilderCreateExpression(DIB, NULL, 0);
    auto variable_list =
        function.GetBlock().GetVariables(extractor->target, true, false, false);
    if (num_function_args != variable_list.GetSize()) {
        LOG_ERROR(
            "function args number dismatch!:value number=%d, function args=%d",
            variable_list.GetSize(), num_function_args);
    }

    LLVMMetadataRef ParamLocation = LLVMDIBuilderCreateDebugLocation(
        comp_ctx->context, line_entry.GetLine(), 0, FunctionMetadata, NULL);

    // TODO:change to void *  or WasmExenv * ?
    LLVMMetadataRef voidtype =
        LLVMDIBuilderCreateBasicType(DIB, "void", 4, 0, 0, LLVMDIFlagZero);
    LLVMMetadataRef voidpionter =
        LLVMDIBuilderCreatePointerType(DIB, voidtype, 64, 0, 0, "void *", 6);

    LLVMMetadataRef ParamVar = LLVMDIBuilderCreateParameterVariable(
        DIB, FunctionMetadata, "exenv", 5, 1,
        File, // starts form 1, and 1 is exenv,
        line_entry.GetLine(), voidpionter, true, LLVMDIFlagZero);
    LLVMValueRef Param = LLVMGetParam(func_ctx->func, 0);
    LLVMBasicBlockRef block_curr = LLVMGetEntryBasicBlock(func_ctx->func);
    LLVMDIBuilderInsertDbgValueAtEnd(DIB, Param, ParamVar, ParamExpression,
                                     ParamLocation, block_curr);

    for (uint32_t function_arg_idx = 0;
         function_arg_idx < variable_list.GetSize(); ++function_arg_idx) {
        SBValue variable(variable_list.GetValueAtIndex(function_arg_idx));
        if (variable.IsValid()) {
            SBDeclaration dec(variable.GetDeclaration());
            auto valtype = variable.GetType();
            LLVMMetadataRef ParamLocation = LLVMDIBuilderCreateDebugLocation(
                comp_ctx->context, dec.GetLine(), dec.GetColumn(),
                FunctionMetadata, NULL);
            LLVMMetadataRef ParamVar = LLVMDIBuilderCreateParameterVariable(
                DIB, FunctionMetadata, variable.GetName(),
                strlen(variable.GetName()), function_arg_idx + 1 + 1,
                File, // starts form 1, and 1 is exenv,
                dec.GetLine(), ParamTypes[function_arg_idx + 1], true,
                LLVMDIFlagZero);
            LLVMValueRef Param =
                LLVMGetParam(func_ctx->func, function_arg_idx + 1);
            LLVMDIBuilderInsertDbgValueAtEnd(DIB, Param, ParamVar,
                                             ParamExpression, ParamLocation,
                                             block_curr);
        }
    }

    return FunctionMetadata;
}

LLVMMetadataRef
dwarf_gen_func_info(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
{
    LLVMMetadataRef func_info = NULL;
    dwar_extractor *extractor;
    uint64_t vm_offset;
    AOTFunc *func = func_ctx->aot_func;

    if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
        return NULL;

    // A code address in DWARF for WebAssembly is the offset of an
    // instruction relative within the Code section of the WebAssembly file.
    // For this reason Section::GetFileAddress() must return zero for the
    // Code section. (refert to ObjectFileWasm.cpp)
    vm_offset = func->code - comp_ctx->comp_data->wasm_module->buf_code;

    auto sbaddr = extractor->target.ResolveFileAddress(vm_offset);
    SBSymbolContext sc(sbaddr.GetSymbolContext(eSymbolContextFunction
                                               | eSymbolContextLineEntry));
    if (sc.IsValid()) {
        SBFunction function(sc.GetFunction());
        if (function.IsValid()) {
            func_info = lldb_function_to_function_dbi(comp_ctx, sc, func_ctx);
        }
    }
    return func_info;
}

void
dwarf_get_func_name(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
                    char *name, int len)
{
    LLVMMetadataRef func_info = NULL;
    dwar_extractor *extractor;
    uint64_t vm_offset;
    AOTFunc *func = func_ctx->aot_func;

    name[0] = '\0';

    if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
        return;

    // A code address in DWARF for WebAssembly is the offset of an
    // instruction relative within the Code section of the WebAssembly file.
    // For this reason Section::GetFileAddress() must return zero for the
    // Code section. (refert to ObjectFileWasm.cpp)
    vm_offset = func->code - comp_ctx->comp_data->wasm_module->buf_code;

    auto sbaddr = extractor->target.ResolveFileAddress(vm_offset);
    SBSymbolContext sc(sbaddr.GetSymbolContext(eSymbolContextFunction
                                               | eSymbolContextLineEntry));
    if (sc.IsValid()) {
        SBFunction function(sc.GetFunction());
        if (function.IsValid()) {
            bh_strcpy_s(name, len, function.GetName());
        }
    }
}

LLVMMetadataRef
dwarf_gen_location(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
                   uint64_t vm_offset)
{
    LLVMMetadataRef location_info = NULL;
    dwar_extractor *extractor;
    AOTFunc *func = func_ctx->aot_func;

    if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
        return NULL;

    auto sbaddr = extractor->target.ResolveFileAddress(vm_offset);
    SBSymbolContext sc(sbaddr.GetSymbolContext(eSymbolContextFunction
                                               | eSymbolContextLineEntry));
    if (sc.IsValid()) {
        // TODO:need to check if the vm_offset is belong to
        SBFunction function(sc.GetFunction());
        if (function.IsValid()) {
            uint64_t start = func_ctx->aot_func->code
                             - comp_ctx->comp_data->wasm_module->buf_code;
            uint64_t end = func_ctx->aot_func->code
                           - comp_ctx->comp_data->wasm_module->buf_code
                           + func_ctx->aot_func->code_size;
            if (function.GetStartAddress().GetOffset() <= start
                && end <= function.GetEndAddress().GetOffset()) {
                auto line_entry = sc.GetLineEntry();
                location_info = LLVMDIBuilderCreateDebugLocation(
                    comp_ctx->context, line_entry.GetLine(),
                    line_entry.GetColumn(), func_ctx->debug_func, NULL);
                // LOG_VERBOSE("Gen the location l:%d, c:%d at %lx",
                // line_entry.GetLine(), line_entry.GetColumn(), vm_offset);
            }
            else
                LOG_WARNING("the offset and function is not matched");
        }
    }
    return location_info;
}

LLVMMetadataRef
dwarf_gen_func_ret_location(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
{
    LLVMMetadataRef func_info = NULL;
    dwar_extractor *extractor;
    uint64_t vm_offset;
    AOTFunc *func = func_ctx->aot_func;
    LLVMMetadataRef location_info = NULL;

    if (!(extractor = TO_EXTACTOR(comp_ctx->comp_data->extractor)))
        return NULL;

    // A code address in DWARF for WebAssembly is the offset of an
    // instruction relative within the Code section of the WebAssembly file.
    // For this reason Section::GetFileAddress() must return zero for the
    // Code section. (refert to ObjectFileWasm.cpp)
    vm_offset = (func->code + func->code_size - 1)
                - comp_ctx->comp_data->wasm_module->buf_code;
    location_info = dwarf_gen_location(comp_ctx, func_ctx, vm_offset);

    return location_info;
}