Skip to content

Commit

Permalink
support dynamic aot debug
Browse files Browse the repository at this point in the history
Signed-off-by: zhangliangyu3 <zhangliangyu3@xiaomi.com>
  • Loading branch information
zhangliangyu3 committed Sep 11, 2024
1 parent 9c2083a commit d772680
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
#define WASM_ENABLE_AOT 0
#endif

#ifndef WASM_ENABLE_DYNAMIC_AOT_DEBUG
#define WASM_ENABLE_DYNAMIC_AOT_DEBUG 0
#endif

#ifndef WASM_ENABLE_WORD_ALIGN_READ
#define WASM_ENABLE_WORD_ALIGN_READ 0
#endif
Expand Down
18 changes: 18 additions & 0 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -4999,6 +4999,18 @@ aot_const_str_set_insert(const uint8 *str, int32 len, AOTModule *module,
return c_str;
}

#if WASM_ENABLE_DYNAMIC_AOT_DEBUG != 0
AOTModule *g_dynamic_aot_module = NULL;

void __attribute__((noinline)) __enable_dynamic_aot_debug(void)
{
/* empty implementation. */
}

void (*__enable_dynamic_aot_debug_ptr)(void)
__attribute__((visibility("default"))) = __enable_dynamic_aot_debug;
#endif

bool
aot_set_module_name(AOTModule *module, const char *name, char *error_buf,
uint32_t error_buf_size)
Expand All @@ -5012,6 +5024,12 @@ aot_set_module_name(AOTModule *module, const char *name, char *error_buf,
false,
#endif
error_buf, error_buf_size);
#if WASM_ENABLE_DYNAMIC_AOT_DEBUG != 0
/* export g_dynamic_aot_module for dynamic aot debug */
g_dynamic_aot_module = module;
/* trigger breakpoint __enable_dynamic_aot_debug */
(*__enable_dynamic_aot_debug_ptr)();
#endif
return module->name != NULL;
}

Expand Down
6 changes: 6 additions & 0 deletions product-mini/platforms/nuttx/wamr.mk
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ else
CFLAGS += -DWASM_ENABLE_WORD_ALIGN_READ=0
endif

ifeq ($(CONFIG_INTERPRETERS_WAMR_AOT_DEBUG),y)
CFLAGS += -DWASM_ENABLE_DYNAMIC_AOT_DEBUG=1
else
CFLAGS += -DWASM_ENABLE_DYNAMIC_AOT_DEBUG=0
endif

ifeq ($(CONFIG_INTERPRETERS_WAMR_MEM_DUAL_BUS_MIRROR),y)
CFLAGS += -DWASM_MEM_DUAL_BUS_MIRROR=1
else
Expand Down
9 changes: 9 additions & 0 deletions product-mini/platforms/posix/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,15 @@ main(int argc, char *argv[])
goto fail2;
}

#if WASM_ENABLE_DYNAMIC_AOT_DEBUG != 0
if (!wasm_runtime_set_module_name(wasm_module, wasm_file, error_buf,
sizeof(error_buf))) {
printf("set aot module name failed in dynamic aot debug mode, %s\n",
error_buf);
goto fail2;
}
#endif

#if WASM_ENABLE_LIBC_WASI != 0
libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx);
#endif
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions test-tools/dynamic-aot-debug/dynamic_aot_debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env python3
#
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#

import os
import gdb

# get object file path from env,set default value "~/objects/"
# exp: export OBJ_PATH=~/work/data/debug/
path_objs = os.getenv("OBJ_PATH", "~/objects/")
# us os.path.expand user expand user path symbol(such as ~)
path_objs = os.path.expanduser(path_objs)
print(f"Object files will be loaded from: {path_objs} on localhost")

aot_module_info = {}


def add_symbol_with_aot_info(aot_module_info):
"""add symbol with aot info"""
text_addr = aot_module_info["code"]
file_name = aot_module_info["name"]

file_name_without_extension, file_extension = os.path.splitext(file_name)

if os.path.sep in file_name_without_extension:
file_name = os.path.basename(file_name_without_extension)

path_symfile = os.path.join(path_objs, file_name_without_extension)

cmd = f"add-symbol-file {path_symfile} {text_addr}"
gdb.execute(cmd)

breakpoints = gdb.execute("info breakpoints", to_string=True)
print("Current breakpoints:", breakpoints)


class read_g_dynamic_aot_module(gdb.Command):
"""read_g_dynamic_aot_module"""

def __init__(self):
super(self.__class__, self).__init__("read_gda", gdb.COMMAND_USER)

def invoke(self, args, from_tty):
"""invoke"""
Aot_module = gdb.parse_and_eval("g_dynamic_aot_module")
found_code = False
found_name = False
Aot_module_fields = []
if Aot_module.type.code == gdb.TYPE_CODE_PTR:
Aot_module = Aot_module.dereference()
if Aot_module.type.strip_typedefs().code == gdb.TYPE_CODE_STRUCT:
Aot_module_fields = [f.name for f in Aot_module.type.fields()]
for field in Aot_module_fields:
var = Aot_module[field]
if field == "name":
aot_module_info["name"] = var.string()
found_name = True
elif field == "code":
aot_module_info["code"] = var.address
found_code = True
if found_code == True and found_name == True:
break
else:
print("Aot_module not struct type!")
else:
print("Aot_module not struct point type!")

add_symbol_with_aot_info(aot_module_info)


def init():
"""init"""
# register the command to gdb
read_g_dynamic_aot_module()
# set a breakpoint at function __enable_dynamic_aot_debug
breakpoint = gdb.Breakpoint("__enable_dynamic_aot_debug")
# attach the self-defined command to the created breakpoint
breakpoint.commands = "read_gda"


init()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d772680

Please sign in to comment.