|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (C) 2021 XiaoMi Corporation. All rights reserved. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | +# |
| 6 | + |
| 7 | +import os |
| 8 | +import gdb |
| 9 | + |
| 10 | +# Get object file path from environment variable or use default value |
| 11 | +path_objs = os.getenv("OBJ_PATH", "~/objects/") |
| 12 | + |
| 13 | +# Expand user directory symbol (~) |
| 14 | +path_objs = os.path.expanduser(path_objs) |
| 15 | +print(f"Object files will be loaded from: {path_objs} on localhost") |
| 16 | + |
| 17 | + |
| 18 | +def add_symbol_with_aot_info(aot_module_info): |
| 19 | + """Add symbol file with AOT information to GDB and list current breakpoints.""" |
| 20 | + try: |
| 21 | + text_addr = aot_module_info.get("code") |
| 22 | + file_name = aot_module_info.get("name") |
| 23 | + |
| 24 | + if not text_addr or not file_name: |
| 25 | + print("Error: 'code' or 'name' missing in AOT module info.") |
| 26 | + return |
| 27 | + |
| 28 | + # Extract base file name without extension |
| 29 | + file_name_without_extension, _ = os.path.splitext(file_name) |
| 30 | + |
| 31 | + # Remove directory part if present |
| 32 | + file_name = os.path.basename(file_name_without_extension) |
| 33 | + |
| 34 | + # Add .obj extension to the file name |
| 35 | + file_name = file_name + ".obj" |
| 36 | + |
| 37 | + # Construct the path for the symbol file |
| 38 | + path_symfile = os.path.join(path_objs, file_name) |
| 39 | + |
| 40 | + # Construct the command to add the symbol file |
| 41 | + cmd = f"add-symbol-file {path_symfile} {text_addr}" |
| 42 | + gdb.execute(cmd) |
| 43 | + |
| 44 | + # Print current breakpoints |
| 45 | + breakpoints = gdb.execute("info breakpoints", to_string=True) |
| 46 | + print("Current breakpoints:", breakpoints) |
| 47 | + |
| 48 | + except gdb.error as e: |
| 49 | + print(f"GDB error: {e}") |
| 50 | + except Exception as e: |
| 51 | + print(f"Unexpected error: {e}") |
| 52 | + |
| 53 | + |
| 54 | +class ReadGDynamicAotModule(gdb.Command): |
| 55 | + """Command to read the g_dynamic_aot_module structure and extract information.""" |
| 56 | + |
| 57 | + def __init__(self): |
| 58 | + super(self.__class__, self).__init__("read_gda", gdb.COMMAND_USER) |
| 59 | + |
| 60 | + def invoke(self, args, from_tty): |
| 61 | + """Retrieve and process the g_dynamic_aot_module structure.""" |
| 62 | + try: |
| 63 | + aot_module = gdb.parse_and_eval("g_dynamic_aot_module") |
| 64 | + aot_module_info = {} |
| 65 | + |
| 66 | + # Ensure aot_module is a pointer and dereference it |
| 67 | + if aot_module.type.code == gdb.TYPE_CODE_PTR: |
| 68 | + aot_module = aot_module.dereference() |
| 69 | + |
| 70 | + # Check if it's a structure type |
| 71 | + if aot_module.type.strip_typedefs().code == gdb.TYPE_CODE_STRUCT: |
| 72 | + for field in aot_module.type.fields(): |
| 73 | + field_name = field.name |
| 74 | + var = aot_module[field_name] |
| 75 | + |
| 76 | + if field_name == "name": |
| 77 | + aot_module_info["name"] = var.string() |
| 78 | + elif field_name == "code": |
| 79 | + aot_module_info["code"] = str(var) |
| 80 | + |
| 81 | + if "name" in aot_module_info and "code" in aot_module_info: |
| 82 | + add_symbol_with_aot_info(aot_module_info) |
| 83 | + else: |
| 84 | + print("Could not find 'name' or 'code' in Aot_module.") |
| 85 | + else: |
| 86 | + print("Aot_module is not of struct type.") |
| 87 | + else: |
| 88 | + print("Aot_module is not a pointer type.") |
| 89 | + except gdb.error as e: |
| 90 | + print(f"An error occurred: {e}") |
| 91 | + |
| 92 | + |
| 93 | +def init(): |
| 94 | + """Initialize environment and set up debugger.""" |
| 95 | + # Register the command to gdb |
| 96 | + ReadGDynamicAotModule() |
| 97 | + |
| 98 | + # Set a breakpoint at function __enable_dynamic_aot_debug |
| 99 | + breakpoint = gdb.Breakpoint("__enable_dynamic_aot_debug") |
| 100 | + # Attach the self-defined command to the created breakpoint, read_gda means read global dynamic aot info. |
| 101 | + breakpoint.commands = "read_gda" |
| 102 | + |
| 103 | + |
| 104 | +init() |
0 commit comments