summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.mi/mi-logging.exp
blob: d50eaee22c86842cf8fa9f8c310da5ad8708517f (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Copyright 2012-2021 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/>.

load_lib mi-support.exp
set MIFLAGS "-i=mi"

standard_testfile basics.c
set opts {debug}

if [build_executable $testfile.exp $testfile $srcfile $opts] {
    untested "failed to compile"
    return -1
}

mi_clean_restart $binfile

if {[mi_runto_main] < 0} {
    return -1
}

set milogfile1 [standard_output_file "milog1.txt"]
set milogfile2 [standard_output_file "milog2.txt"]

remote_file host delete $milogfile1
remote_file host delete $milogfile2

mi_gdb_test "-gdb-set logging file $milogfile1" ".*"

mi_gdb_test "-gdb-set logging overwrite on" ".*"

mi_gdb_test "-gdb-set logging on" ".*" "logging on"

mi_step "logged step"

mi_next "logged next"

mi_gdb_test "-gdb-set logging off" ".*" "logging off"

set chan [open $milogfile1]
set logcontent [read $chan]
close $chan

set mi_log_prompt "\[(\]gdb\[)\] \[\r\n\]+"

if [regexp "\\^done\[\r\n\]+$mi_log_prompt\\^running\[\r\n\]+\\*running,thread-id=\"all\"\[\r\n\]+$mi_log_prompt\\*stopped,reason=\"end-stepping-range\",.*\[\r\n\]+$mi_log_prompt\\^running\[\r\n\]+\\*running,thread-id=\"all\"\[\r\n\]+$mi_log_prompt\\*stopped,reason=\"end-stepping-range\",.*\[\r\n\]+$mi_log_prompt" $logcontent] {
    pass "log file contents"
} else {
    fail "log file contents"
}

# Now try the redirect, which writes into the file only.

mi_gdb_test "-gdb-set logging file $milogfile2" ".*"

mi_gdb_test "999-gdb-set logging redirect on" ".*" "redirect logging on"

send_gdb "1001-gdb-set logging on\n"

# We can't open the file right away, because we're not waiting for
# GDB's "^done" -- we could end up opening the file before GDB creates
# it.  So spin waiting until the file exists.

proc wait_open {filename} {
    set ticks [expr 10 * $::timeout]
    set t 0

    while {![file exists $filename]} {
	after 100

	incr t
	if {$t == $ticks} {
	    return ""
	}
    }
    return [open $filename]
}

set chan [wait_open $milogfile2]

# Read GDB output in channel/file CHAN, expect the result.  PATTERN is
# the pattern to match for a PASS.

proc gdb_test_file {chan pattern test} {
    global timeout

    verbose -log "gdb_test_file: begin"

    set ticks [expr 10 * $timeout]
    set t 0
    set logcontent ""

    while {1} {
	set r [read $chan]
	append logcontent $r

	send_log -- "$r"

	if [regexp "$pattern" $logcontent] {
	    pass "$test"
	    break
	} else {
	    incr t
	    if {$t == $ticks} {
		fail "$test (timeout)"
		break
	    }
	    after 100
	}
    }

    verbose -log "gdb_test_file: end"
}

gdb_test_file $chan \
    "1001\\^done\[\r\n\]+$mi_log_prompt" \
    "redirect log file contents, set logging on"

send_gdb "1002-exec-step\n"
gdb_test_file $chan \
    "1002\\^running\[\r\n\]+\\*running,thread-id=\"all\"\[\r\n\]+$mi_log_prompt\\*stopped,reason=\"end-stepping-range\",.*\[\r\n\]+$mi_log_prompt" \
    "redirect log file contents, exec-step"

send_gdb "1003-exec-next\n"
gdb_test_file $chan \
    "1003\\^running\[\r\n\]+\\*running,thread-id=\"all\"\[\r\n\]+$mi_log_prompt\\*stopped,reason=\"end-stepping-range\",.*\[\r\n\]+$mi_log_prompt" \
    "redirect log file contents, exec-next"

close $chan

mi_gdb_test "1004-gdb-set logging off" ".*" "redirect logging off"

# Now try enabling a redirect while GDB is already logging.  This used
# to crash GDB.
with_test_prefix "redirect while already logging" {
    mi_gdb_test "-gdb-set logging redirect off" ".*" \
	"logging redirect off"
    mi_gdb_test "-gdb-set logging on" ".*" \
	"logging on"
    mi_gdb_test "-gdb-set logging redirect on" \
	".*warning: Currently logging .*Turn the logging off and on to make the new setting effective.*" \
	"logging redirect on"
    mi_gdb_test "-gdb-set logging off" ".*" \
	"logging off"
}

mi_gdb_exit