aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/boards/simavr.exp
blob: 1bd02261f50187c7e2863202fac7031b62c0025e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Copyright 2020 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Let the user override the path to the simavr binary with the SIMAVR_PATH
# environment variable.

if { [info exists ::env(SIMAVR_PATH)] } {
    set simavr_path $::env(SIMAVR_PATH)
} else {
    set simavr_path simavr
}

# Let the user override the simulated AVR chip with the SIMAVR_PATH environment
# variable.
#
# The value passed here must be supported by avr-gcc (see the -mmcu flag in the
# `AVR Options` section of the gcc(1) man page) and by simavr (see output of
# `simavr --list-cores`).

if { [info exists ::env(SIMAVR_MCU)] } {
    set simavr_mcu $::env(SIMAVR_MCU)
} else {
    set simavr_mcu atmega2560
}

set simavr_last_load_file ""
set simavr_spawn_id ""

set_board_info compiler avr-gcc
set_board_info c++compiler avr-g++

set_board_info cflags "-mmcu=${simavr_mcu}"
set_board_info ldflags "-mmcu=${simavr_mcu}"

set_board_info use_gdb_stub 1
set_board_info gdb_protocol "remote"
set_board_info gdb,do_reload_on_run 1
set_board_info noargs 1
set_board_info gdb,noinferiorio 1
set_board_info gdb,nofileio 1
set_board_info gdb,noresults 1
set_board_info gdb,nosignals 1

proc gdb_load { file } {
    global simavr_last_load_file
    global simavr_spawn_id
    global simavr_mcu
    global simavr_path
    global gdb_prompt

    if { $file == "" } {
	set file $simavr_last_load_file
    } else {
	set simavr_last_load_file $file
    }

    gdb_file_cmd $file

    # Close any previous simavr instance.
    if { $simavr_spawn_id != "" } {
	verbose -log "simavr: closing previous spawn id $simavr_spawn_id"
	if [catch { close -i $simavr_spawn_id } != 0] {
	    warning "simavr: failed to close connection to previous simavr instance"
	}

	wait -i $simavr_spawn_id
	set simavr_spawn_id ""
    }

    # Run simavr.
    set cmd "spawn -noecho ${simavr_path} --mcu ${simavr_mcu} -g $file"
    verbose -log "Spawning simavr: $cmd"
    eval $cmd
    set simavr_spawn_id $spawn_id

    verbose -log "simavr: simavr spawned with spawn id $simavr_spawn_id, pid [exp_pid $simavr_spawn_id]"

    # Wait for "listening on port" message of simavr.
    expect {
	-i $simavr_spawn_id -re ".*avr_gdb_init listening on port 1234" {}
	timeout {
	    verbose -log "simavr: timeout, closing simavr spawn id"
	    close -i $simavr_spawn_id
	    verbose -log "simavr: timeout, waiting for simavr process exit"
	    wait -i $simavr_spawn_id
	    set simavr_spawn_id ""
	    error "unable to start simavr: timeout"
	}
	eof {
	    verbose -log "simavr: eof, waiting for simavr process exit"
	    wait -i $simavr_spawn_id
	    set simavr_spawn_id ""
	    error "unable to start simavr: eof"
	}
    }

    # Connect to simavr.
    send_gdb "target remote :1234\n"
    gdb_expect {
	-re ".*Remote debugging using :1234.*\[\r\n\]+$gdb_prompt $" {}
	timeout {
	    verbose -log "simavr: unable to connect to simavr, closing simavr spawn id"
	    close -i $simavr_spawn_id
	    verbose -log "simavr: unable to connect to simavr, waiting for simavr process exit"
	    wait -i $simavr_spawn_id
	    set simavr_spawn_id ""
	    error "unable to connect to simavr stub"
	}
    }

    return 0
}