validate_lldb.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (C) 2023 Intel Corporation. All rights reserved.
  4. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5. #
  6. import argparse
  7. import time
  8. from pathlib import Path
  9. import subprocess, shlex
  10. SCRIPT_DIR = Path(__file__).parent.resolve()
  11. REPO_ROOT_DIR = SCRIPT_DIR.parent
  12. SAMPLE_CODE_FILE = REPO_ROOT_DIR / 'product-mini/app-samples/hello-world/main.c'
  13. WASM_OUT_FILE = SCRIPT_DIR / 'out.wasm'
  14. parser = argparse.ArgumentParser(
  15. description="Validate the customized lldb with sample code"
  16. )
  17. parser.add_argument(
  18. "-l", "--lldb", dest='lldb', default='lldb', help="path to lldb executable"
  19. )
  20. parser.add_argument(
  21. "-w", "--wamr", dest='wamr', default='iwasm', help="path to iwasm executable"
  22. )
  23. parser.add_argument(
  24. "-p", "--port", dest='port', default='1234', help="debug server listen port"
  25. )
  26. parser.add_argument(
  27. "-v", "--verbose", dest='verbose', action='store_true', default=False, help="display lldb stdout"
  28. )
  29. options = parser.parse_args()
  30. lldb_command_epilogue = '-o q'
  31. test_cases = {
  32. 'run_to_exit': '-o c',
  33. 'func_breakpoint': '-o "b main" -o c -o c',
  34. 'line_breakpoint': '-o "b main.c:12" -o c -o c',
  35. 'break_on_unknown_func': '-o "b not_a_func" -o c',
  36. 'watch_point': '-o "b main" -o c -o "watchpoint set variable buf" -o c -o "fr v buf" -o c',
  37. }
  38. # Step1: Build wasm module with debug information
  39. build_cmd = f'/opt/wasi-sdk/bin/clang -g -O0 -o {WASM_OUT_FILE} {SAMPLE_CODE_FILE}'
  40. try:
  41. print(f'building wasm module ...', end='', flush=True)
  42. subprocess.check_call(shlex.split(build_cmd))
  43. print(f'\t OK')
  44. except subprocess.CalledProcessError:
  45. print("Failed to build wasm module with debug information")
  46. exit(1)
  47. def print_process_output(p):
  48. try:
  49. outs, errs = p.communicate(timeout=2)
  50. print("stdout:")
  51. print(outs)
  52. print("stderr:")
  53. print(errs)
  54. except subprocess.TimeoutExpired:
  55. print("Failed to get process output")
  56. # Step2: Launch WAMR in debug mode and validate lldb commands
  57. iteration = 0
  58. for case, cmd in test_cases.items():
  59. lldb_command_prologue = f'{options.lldb} -o "process connect -p wasm connect://127.0.0.1:{int(options.port) + iteration}"'
  60. wamr_cmd = f'{options.wamr} -g=127.0.0.1:{int(options.port) + iteration} {WASM_OUT_FILE}'
  61. iteration += 1
  62. has_error = False
  63. print(f'validating case [{case}] ...', end='', flush=True)
  64. lldb_cmd = f'{lldb_command_prologue} {cmd} {lldb_command_epilogue}'
  65. wamr_process = subprocess.Popen(shlex.split(
  66. wamr_cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
  67. time.sleep(0.1)
  68. if (wamr_process.poll() != None):
  69. print("\nWAMR doesn't wait for lldb connection")
  70. print_process_output(wamr_process)
  71. exit(1)
  72. lldb_process = subprocess.Popen(shlex.split(
  73. lldb_cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
  74. if (options.verbose):
  75. while (lldb_process.poll() is None):
  76. print(lldb_process.stdout.read(), end='', flush=True)
  77. try:
  78. if (lldb_process.wait(5) != 0):
  79. print(f"\nFailed to validate case [{case}]")
  80. print_process_output(lldb_process)
  81. has_error = True
  82. if wamr_process.wait(2) != 0:
  83. print("\nWAMR process doesn't exit normally")
  84. print_process_output(wamr_process)
  85. has_error = True
  86. except subprocess.TimeoutExpired:
  87. print(f"\nFailed to validate case [{case}]")
  88. print("wamr output:")
  89. print_process_output(wamr_process)
  90. print("lldb output:")
  91. print_process_output(lldb_process)
  92. has_error = True
  93. finally:
  94. if (lldb_process.poll() == None):
  95. print(f'\nterminating lldb process [{lldb_process.pid}]')
  96. lldb_process.kill()
  97. if (wamr_process.poll() == None):
  98. print(f'terminating wamr process [{wamr_process.pid}]')
  99. wamr_process.kill()
  100. if (has_error):
  101. exit(1)
  102. print(f'\t OK')
  103. # wait 100ms to ensure the socket is closed
  104. time.sleep(0.1)
  105. print('Validate lldb success')
  106. exit(0)