simavr.exp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright 2020-2022 Free Software Foundation, Inc.
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 3 of the License, or
  5. # (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. # Let the user override the path to the simavr binary with the SIMAVR_PATH
  15. # environment variable.
  16. if { [info exists ::env(SIMAVR_PATH)] } {
  17. set simavr_path $::env(SIMAVR_PATH)
  18. } else {
  19. set simavr_path simavr
  20. }
  21. # Let the user override the simulated AVR chip with the SIMAVR_PATH environment
  22. # variable.
  23. #
  24. # The value passed here must be supported by avr-gcc (see the -mmcu flag in the
  25. # `AVR Options` section of the gcc(1) man page) and by simavr (see output of
  26. # `simavr --list-cores`).
  27. if { [info exists ::env(SIMAVR_MCU)] } {
  28. set simavr_mcu $::env(SIMAVR_MCU)
  29. } else {
  30. set simavr_mcu atmega2560
  31. }
  32. set simavr_last_load_file ""
  33. set simavr_spawn_id ""
  34. set_board_info compiler avr-gcc
  35. set_board_info c++compiler avr-g++
  36. set_board_info cflags "-mmcu=${simavr_mcu}"
  37. set_board_info ldflags "-mmcu=${simavr_mcu}"
  38. # As of version 10, GCC produces stabs by default for AVR. Force it to use
  39. # DWARF.
  40. set_board_info debug_flags "-gdwarf-4"
  41. set_board_info use_gdb_stub 1
  42. set_board_info gdb_protocol "remote"
  43. set_board_info gdb,do_reload_on_run 1
  44. set_board_info noargs 1
  45. set_board_info gdb,noinferiorio 1
  46. set_board_info gdb,nofileio 1
  47. set_board_info gdb,noresults 1
  48. set_board_info gdb,nosignals 1
  49. proc gdb_load { file } {
  50. global simavr_last_load_file
  51. global simavr_spawn_id
  52. global simavr_mcu
  53. global simavr_path
  54. global gdb_prompt
  55. if { $file == "" } {
  56. set file $simavr_last_load_file
  57. } else {
  58. set simavr_last_load_file $file
  59. }
  60. gdb_file_cmd $file
  61. # Close any previous simavr instance.
  62. if { $simavr_spawn_id != "" } {
  63. verbose -log "simavr: closing previous spawn id $simavr_spawn_id"
  64. if [catch { close -i $simavr_spawn_id } != 0] {
  65. warning "simavr: failed to close connection to previous simavr instance"
  66. }
  67. wait -i $simavr_spawn_id
  68. set simavr_spawn_id ""
  69. }
  70. # Run simavr.
  71. set cmd "spawn -noecho ${simavr_path} --mcu ${simavr_mcu} -g $file"
  72. verbose -log "Spawning simavr: $cmd"
  73. eval $cmd
  74. set simavr_spawn_id $spawn_id
  75. verbose -log "simavr: simavr spawned with spawn id $simavr_spawn_id, pid [exp_pid $simavr_spawn_id]"
  76. # Wait for "listening on port" message of simavr.
  77. expect {
  78. -i $simavr_spawn_id -re ".*avr_gdb_init listening on port 1234" {}
  79. timeout {
  80. verbose -log "simavr: timeout, closing simavr spawn id"
  81. close -i $simavr_spawn_id
  82. verbose -log "simavr: timeout, waiting for simavr process exit"
  83. wait -i $simavr_spawn_id
  84. set simavr_spawn_id ""
  85. error "unable to start simavr: timeout"
  86. }
  87. eof {
  88. verbose -log "simavr: eof, waiting for simavr process exit"
  89. wait -i $simavr_spawn_id
  90. set simavr_spawn_id ""
  91. error "unable to start simavr: eof"
  92. }
  93. }
  94. # Connect to simavr.
  95. send_gdb "target remote :1234\n"
  96. gdb_expect {
  97. -re ".*Remote debugging using :1234.*\[\r\n\]+$gdb_prompt $" {}
  98. timeout {
  99. verbose -log "simavr: unable to connect to simavr, closing simavr spawn id"
  100. close -i $simavr_spawn_id
  101. verbose -log "simavr: unable to connect to simavr, waiting for simavr process exit"
  102. wait -i $simavr_spawn_id
  103. set simavr_spawn_id ""
  104. error "unable to connect to simavr stub"
  105. }
  106. }
  107. return 0
  108. }