dynamic_aot_debug.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. import os
  7. import gdb
  8. # Get object file path from environment variable or use default value
  9. path_objs = os.getenv("OBJ_PATH", "~/objects/")
  10. # Expand user directory symbol (~)
  11. path_objs = os.path.expanduser(path_objs)
  12. print(f"Object files will be loaded from: {path_objs} on localhost")
  13. def add_symbol_with_aot_info(aot_module_info):
  14. """Add symbol file with AOT information to GDB and list current breakpoints."""
  15. try:
  16. text_addr = aot_module_info.get("code")
  17. file_name = aot_module_info.get("name")
  18. if not text_addr or not file_name:
  19. print("Error: 'code' or 'name' missing in AOT module info.")
  20. return
  21. # Extract base file name without extension
  22. file_name_without_extension, _ = os.path.splitext(file_name)
  23. # Remove directory part if present
  24. file_name = os.path.basename(file_name_without_extension)
  25. # Add .obj extension to the file name
  26. file_name = file_name + ".obj"
  27. # Construct the path for the symbol file
  28. path_symfile = os.path.join(path_objs, file_name)
  29. # Construct the command to add the symbol file
  30. cmd = f"add-symbol-file {path_symfile} {text_addr}"
  31. gdb.execute(cmd)
  32. # Print current breakpoints
  33. breakpoints = gdb.execute("info breakpoints", to_string=True)
  34. print("Current breakpoints:", breakpoints)
  35. except gdb.error as e:
  36. print(f"GDB error: {e}")
  37. except Exception as e:
  38. print(f"Unexpected error: {e}")
  39. class ReadGDynamicAotModule(gdb.Command):
  40. """Command to read the g_dynamic_aot_module structure and extract information."""
  41. def __init__(self):
  42. super(self.__class__, self).__init__("read_gda", gdb.COMMAND_USER)
  43. def invoke(self, args, from_tty):
  44. """Retrieve and process the g_dynamic_aot_module structure."""
  45. try:
  46. aot_module = gdb.parse_and_eval("g_dynamic_aot_module")
  47. aot_module_info = {}
  48. # Ensure aot_module is a pointer and dereference it
  49. if aot_module.type.code == gdb.TYPE_CODE_PTR:
  50. aot_module = aot_module.dereference()
  51. # Check if it's a structure type
  52. if aot_module.type.strip_typedefs().code == gdb.TYPE_CODE_STRUCT:
  53. for field in aot_module.type.fields():
  54. field_name = field.name
  55. var = aot_module[field_name]
  56. if field_name == "name":
  57. aot_module_info["name"] = var.string()
  58. elif field_name == "code":
  59. aot_module_info["code"] = str(var)
  60. if "name" in aot_module_info and "code" in aot_module_info:
  61. add_symbol_with_aot_info(aot_module_info)
  62. else:
  63. print("Could not find 'name' or 'code' in Aot_module.")
  64. else:
  65. print("Aot_module is not of struct type.")
  66. else:
  67. print("Aot_module is not a pointer type.")
  68. except gdb.error as e:
  69. print(f"An error occurred: {e}")
  70. def init():
  71. """Initialize environment and set up debugger."""
  72. # Register the command to gdb
  73. ReadGDynamicAotModule()
  74. # Set a breakpoint at function __enable_dynamic_aot_debug
  75. breakpoint = gdb.Breakpoint("__enable_dynamic_aot_debug")
  76. # Attach the self-defined command to the created breakpoint, read_gda means read global dynamic aot info.
  77. breakpoint.commands = "read_gda"
  78. init()